跨源资源共享的问题:预检请求后,OSX Safari和iOS Safari均失败 [英] problems with Cross Origin Resource Sharing: both OSX Safari and iOS Safari fail after preflight request
问题描述
最近,我在Safari(OSX和iOS)上都遇到了CORS(跨源资源共享)请求的麻烦,而相同的请求在Chrome和Firefox上却能完美运行。我遵循 W3C的文档并在服务器端处理预检请求,我的响应是: / p>
Recently I come into a trouble with CORS(Cross Origin Resource Sharing) request on Safari, both OSX and iOS, while the same request works perfectly on Chrome and Firefox. I follow the documentation of W3C and handle preflight request on server side, my response is like:
HTTP/1.0 200 OK
Access-Control-Allow-Origin: http://192.168.1.96:4399
Access-Control-Allow-Methods: POST
Access-Control-Allow-Methods: GET
Access-Control-Allow-Methods: OPTIONS
Access-Control-Allow-Headers: Origin
Access-Control-Allow-Headers: Authorization
Access-Control-Allow-Headers: X-Requested-With
Access-Control-Allow-Headers: Accept
Access-Control-Allow-Headers: Access-Control-Request-Method
Access-Control-Allow-Headers: Access-Control-Request-Headers
Access-Control-Allow-Headers: DNT
Access-Control-Allow-Headers: X-CustomHeader
Access-Control-Allow-Headers: Content-Type
Access-Control-Max-Age: 0
Date: Fri, 25 Nov 2016 08:45:25 GMT
Origin: http://192.168.1.96:4399
Access-Control-Expose-Headers: Origin
Content-Encoding: gzip
Transfer-Encoding: chunked
这种响应在Chrome上效果很好, Firefox和Android浏览器:在预检请求之后立即发送POST请求。但是在Safari上,服务器响应请求请求后,我从控制台收到了以下错误消息:
Such response works fine on Chrome, Firefox, and Android Browser: a POST request is sent right after the preflight request. But on Safari, after server response the reflight request, I got such error message from console:
Failed to load resource: The network connection was lost.
我检查了来自服务器的飞行前响应,但发现与上述相同...搜索并尝试了很多次,我仍然无法使它起作用:-(
以前有人遇到过这个问题吗?有人能找出我犯了什么错误吗?
非常感谢!
I inspect the preflight response from server, but find it the same as above... After searching and trying so many times, I still can't make it work :-(
Is there anybody that come across the problem before? Could anyone figure out what mistake I've made?
Thanks very much in advance!
推荐答案
所有关于Safari的功能 Access-Control-Request-Headers
。
It's all about Safari add origin
to OPTIONS request header Access-Control-Request-Headers
.
因此,要对其进行修复,应启用
So to fix it you should enable this header in Access-Control-Allow-Headers
in response.
PS:请参见类似的问题CORS请求在Safari中不起作用
PS: see similar question CORS request not working in Safari
这篇关于跨源资源共享的问题:预检请求后,OSX Safari和iOS Safari均失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!