Jquery Ajax 使用 CORS 总是抛出错误
只是为了澄清,因为我无法在标题中容纳足够的信息...
我正在执行跨站点请求并遵守 CORS,并且一切正常。浏览器发送一个选项请求,我的服务以一些接受标头进行响应,然后浏览器接受 200 响应并发送帖子。如果您不需要来自标头输出的任何信息,请随意跳过标头输出:
初始选项请求:
OPTIONS http://localhost:1837/authentication HTTP/1.1
Host: localhost:1837
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Origin: http://localhost:6879
Access-Control-Request-Method: POST
Access-Control-Request-Headers: x-requested-with
Pragma: no-cache
Cache-Control: no-cache
选项响应:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 30 Jun 2011 09:50:43 GMT
X-AspNet-Version: 4.0.30319
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: CONTENT-TYPE, x-requested-with
Access-Control-Allow-Origin: http://localhost:6879
X-AspNetMvc-Version: 3.0
Cache-Control: private
Content-Length: 0
Connection: Close
发布请求:
POST http://localhost:1837/authentication HTTP/1.1
Host: localhost:1837
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: */*
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:6879/login
Content-Length: 27
Origin: http://localhost:6879
Pragma: no-cache
Cache-Control: no-cache
username=test&password=test
发布响应:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 30 Jun 2011 09:50:43 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 3.0
Set-Cookie: .ASPXAUTH=A22B60AC3F317F90A263217B50548A1D43871D43BD84A160C2AC4DCCF4EF33F2E044A9FCEC64AFCF0539652684BF5D1B648F12F92E0788096A9F5BE7036E386D8EE262A6FA6A922446F3C114E6FC1AFB990AADB901B778C7735B4B1EBD6F967EA7B27E5780AFA0037A9BF59B5CBBD78A60DC8E9FDE3D0A0BBB6DEE31B38D62944247F19BF8052B3DCFB6B6AC6DEC886EC5B8FBE23ABBB7485E1767FCCF1DDEF8CC99253F1068C33946A625CD6462EF80B4515B5CDE54E26571C3EB38ACB7DD6B0EDDC9AAD58DA878CB1BC1E5242E8FD2C62FD9FE1DCEE527700E1B68B8BC4BC3DBF5775B3ADD46C728841312DAE1F864E05D61B0375F99AF307831EB210BDA995369FBE2CEC16F5F97EAA3BB1E08E0A2D8DE22EB6DF69F5732946A449D76080297E2CBB0953F941F10778605FBFB9A8B; domain=localhost; expires=Thu, 30-Jun-2011 10:20:43 GMT; path=/
Cache-Control: private
Content-Length: 0
Connection: Close
一旦这一切完成,我的 jquery 错误回调将被调用,状态文本为“错误”,并且 xhr 包含:
readyState 0
responseText ""
status 0
statusText "error"
我有点困惑,因为响应似乎有效,并且 xhr 甚至不包含任何状态代码。我尝试查看 jquery 完整回调和其中的相同详细信息。有人见过这样的事情吗?哦,我正在使用 Firefox,但也在 Chrome 中进行了测试,遇到同样的问题:(
Just to clarify as I cannot fit enough info in the heading...
I am doing a cross site request and adhering to CORS, and that all works. The browser sends an options request, my service responds with a sprinkling of acceptance headers, then the browser accepts the 200 response and sends the post. Feel free to skip the header outputs if you dont need any info from them:
Initial Options Request:
OPTIONS http://localhost:1837/authentication HTTP/1.1
Host: localhost:1837
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Origin: http://localhost:6879
Access-Control-Request-Method: POST
Access-Control-Request-Headers: x-requested-with
Pragma: no-cache
Cache-Control: no-cache
Options Response:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 30 Jun 2011 09:50:43 GMT
X-AspNet-Version: 4.0.30319
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: CONTENT-TYPE, x-requested-with
Access-Control-Allow-Origin: http://localhost:6879
X-AspNetMvc-Version: 3.0
Cache-Control: private
Content-Length: 0
Connection: Close
Post request:
POST http://localhost:1837/authentication HTTP/1.1
Host: localhost:1837
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: */*
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:6879/login
Content-Length: 27
Origin: http://localhost:6879
Pragma: no-cache
Cache-Control: no-cache
username=test&password=test
Post response:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 30 Jun 2011 09:50:43 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 3.0
Set-Cookie: .ASPXAUTH=A22B60AC3F317F90A263217B50548A1D43871D43BD84A160C2AC4DCCF4EF33F2E044A9FCEC64AFCF0539652684BF5D1B648F12F92E0788096A9F5BE7036E386D8EE262A6FA6A922446F3C114E6FC1AFB990AADB901B778C7735B4B1EBD6F967EA7B27E5780AFA0037A9BF59B5CBBD78A60DC8E9FDE3D0A0BBB6DEE31B38D62944247F19BF8052B3DCFB6B6AC6DEC886EC5B8FBE23ABBB7485E1767FCCF1DDEF8CC99253F1068C33946A625CD6462EF80B4515B5CDE54E26571C3EB38ACB7DD6B0EDDC9AAD58DA878CB1BC1E5242E8FD2C62FD9FE1DCEE527700E1B68B8BC4BC3DBF5775B3ADD46C728841312DAE1F864E05D61B0375F99AF307831EB210BDA995369FBE2CEC16F5F97EAA3BB1E08E0A2D8DE22EB6DF69F5732946A449D76080297E2CBB0953F941F10778605FBFB9A8B; domain=localhost; expires=Thu, 30-Jun-2011 10:20:43 GMT; path=/
Cache-Control: private
Content-Length: 0
Connection: Close
Once thats all done my jquery error callback is called with the statusText "error" and the xhr contains:
readyState 0
responseText ""
status 0
statusText "error"
I am a bit baffled as the response seems valid, and the xhr doesn't even contain any status code. I have tried looking in the jquery complete callback and same details in there. Anyone seen anything like this before? Oh and I am using Firefox but have tested in Chrome too and same issue :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的发布响应仍然需要 Access-Control-Allow-Origin 标头(所有 CORS 响应都需要它,因为 HTTP 是无状态的)。
另外,关于错误报告,我发现浏览器对 CORS 错误的响应并不是很有帮助。希望这个问题将来能够得到解决。
Your post response still needs the Access-Control-Allow-Origin header (it is required for all CORS responses, since HTTP is stateless).
Also regarding error reporting, I've found that browser responses to CORS errors aren't very helpful. Hopefully this will be fixed in the future.