我正在尝试使用以下代码发出 AJAX 请求以从 S3 下载 JSON 文件:
$.ajax({
'type':'GET',
'url':this.baseUrl + publicationId + "/document.json",
'crossDomain': true,
'dataType':'json',
'success':function(data) {
console.log('got document.json');
}
});
这在 Firefox 中工作正常,如果 baseUrl 是 http://
XMLHttpRequest cannot load https ://s3.amazonaws.com/<bucket-name>/<pub-id>/document.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https ://localhost.office.lucidpress.com' is therefore not allowed access.
使用 CURL 发出相同的请求确实具有以下 header :“Access-Control-Allow-Origin: *”。
为什么这不适用于 Chrome,有什么解决方法吗?
注意:我在 URL 中放置了空格以防止它们成为链接。
最佳答案
Chrome 将发送飞行前 OPTIONS 请求以查看是否可以执行 GET。如果你 curl -X OPTIONS -i https://s3.amazonaws.com/<bucket-name>/<pub-id>/document.json
你会收到什么标题?您很可能需要添加 Access-Control-Allow-Headers
您的服务器响应还允许 Chrome 可能在 OPTIONS 请求上发送任何额外的 header ,即 Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
或者,由于这是一个 https 请求,您的答案可能是 Why Chrome cancel CORS OPTION request
关于ajax - Chrome : GET request cancelled for CORS HTTPS request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24516300/