最近我在Safari(OSX和iOS)上遇到了CORS(跨源资源共享)请求的问题,而同样的请求在Chrome和Firefox上完美运行。我按照the documentation of W3C处理服务器端的预检请求,我的回答是:
require 'immutable-struct'
module ImmutableStructWithId
def self.new(*args)
ImmutableStruct.new(*args) do
def hash; id; end
end
end
end
Campus = ImmutableStructWithId.new(:id, :name, :timezone)
campus = Campus.new(id: '1', name: 'foo', timezone: 'UTC')
#=> #<Campus:0x007f8ed581de20 @id="1", @name="foo", @timezone="UTC">
campus.hash
#=> "1"
此类响应在Chrome,Firefox和Android浏览器上正常运行:在预检请求后立即发送POST请求。但是在Safari上,在服务器响应了反射请求后,我从控制台收到了这样的错误消息:
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
我检查来自服务器的预检响应,但发现它与上面相同...经过多次搜索和尝试后,我仍然无法使其工作:-(
以前有人遇到过这个问题吗?谁能弄明白我犯了什么错?
非常感谢提前!
答案 0 :(得分:2)
所有关于Safari将origin
添加到OPTIONS请求标头Access-Control-Request-Headers
。
因此,要修复它,您应该在Access-Control-Allow-Headers
中启用此标头作为回应。
PS:看到类似的问题CORS request not working in Safari