Angularjs $http 发送 OPTIONS 而不是 PUT

Angularjs $http send OPTIONS instead PUT

我知道浏览器首先向服务器发送一个OPTIONS。但是我在 Angularjs 1.5 中遇到了这个问题。我正在尝试发送一个 PUT 请求,它正在发送一个 OPTIONS。为什么这很奇怪?,因为我正在使用一个应用程序来测试我所有的 urls 并且 url 使用 PUT 可以正常工作。

let fd = new FormData()
let d
for (d in $scope.uploadAutos) {
  fd.append(d, $scope.uploadAutos[d])
}

$http({
     method: 'PUT',
    url: `http://sitio.api.com/vehiculo/${parseInt($routeParams.id, 10)}`,
    data: fd,
    headers: { 'Content-Type': 'undefined' }
   }).then(success => {
     console.log(success)
   }, error => {
     console.log(error)
  }
)

到目前为止,这是我一直在处理 GET 和 POST 请求的方式,它工作正常...但是对于 PUT...嗯有些地方不工作。后端对于上面所说的是正确的,使用休息 api 应用程序来测试我所有的 url,PUT url 工作正常。任何帮助都会很棒 :)。谢谢

理想情况下,您会看到两个请求 - OPTIONS 请求,即 pre-flight 请求。这是为了验证您的跨源请求 headers.

验证后,您的客户将能够提出实际的 POST/PUT 请求。如果您的 pre-flight 请求失败,这意味着您的 CORS 请求 headers 没有在您的服务器上正确设置。

需要设置以下headers:

Access-Control-Allow-Origin
Access-Control-Allow-Methods ("GET, POST, PUT, DELETE, OPTIONS")
Access-Control-Allow-Headers
Access-Control-Max-Age

这就是我在飞行前寻找的词!!哈哈。好的,我已经开始工作了。第一的。我这边缺乏信息。所以我意识到问题不在 angular 中,不在我的后端中,问题出在 slim php 的文档中,据我所知。当你在 map 函数中使用 PUT 或 DELETE 时,你必须添加 OPTIONS,这是因为

Mozilla 有答案

The Cross-Origin Resource Sharing standard works by adding new HTTP headers that allow servers to describe the set of origins that are permitted to read that information using a web browser. Additionally, for HTTP request methods that can cause side-effects on user data (in particular, for HTTP methods other than GET, or for POST usage with certain MIME types), the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS request method, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method. Servers can also notify clients whether "credentials" (including Cookies and HTTP Authentication data) should be sent with requests.