选项请求成功但仍然获得Access-Control-Allow-Origin错误

时间:2013-05-27 20:19:23

标签: javascript ajax rest backbone.js xmlhttprequest

我正在从Backbone应用程序发出DELETE请求,并且不确定它为什么会因下面显示的错误而失败。

选项标题

这是我的OPTIONS标题,它是200次成功

请求:

OPTIONS /revenues/674sf6 HTTP/1.1
Host: api2.revparmax.local
Connection: keep-alive
Access-Control-Request-Method: DELETE
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) ....
Access-Control-Request-Headers: token, accept, origin, x-requested-with, content-type
Accept: */*
Referer: http://localhost:3000/app/daily/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

响应:

HTTP/1.1 200 OK
Date: Mon, 27 May 2013 20:01:32 GMT
Server: Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1...
X-Powered-By: PHP/5.3.1
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: origin, x-requested-with, content-type, accept, TOKEN
Access-Control-Max-Age: 86400
Content-Length: 2
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8

DELETE标题

但是我的删除请求失败了......

请求:

DELETE http://api2.revparmax.local/revenues/674sf6 HTTP/1.1
TOKEN: 00899766786875875674557454
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost:3000/app/daily/
Origin: http://localhost:3000
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) ....
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

错误

XMLHttpRequest cannot load http://api2.revparmax.local/revenues/674sf6. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.

注意

哦 - 我应该提一下这个模型仍然会从服务器上删除但我的问题是我的骨干应用程序中延迟的Ajax因为这个错误而全部被抛弃。

另外,我失败时的请求有效负载是[object Object] - 这可能导致问题吗?

1 个答案:

答案 0 :(得分:1)

你在运行Chrome吗?如果是这样,那么您可能已经遇到了CORS localhost错误。见this SO question