ajax - Chrome : GET request cancelled for CORS HTTPS request

标签 ajax json google-chrome amazon-s3 cors

我正在尝试使用以下代码发出 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://,它工作,但如果它是 https://,Chrome 会取消请求并出现错误:

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/

相关文章:

javascript - 通过数据 uri 下载包含换行符的 json

sql - 选择表作为 json 数组

google-chrome - Greasemonkey @require 在 Chrome 中不起作用

html - 交叉浏览 : OpenSans Chrome VS Safari

javascript - 为什么 document.getElementsByClassName 不断返回未定义的数组?

ajax - 如果浏览操作,则在Grails中重定向用户

java - 使用保留变量将 Json 转换为 Java 对象

php - 在 Windows 调整大小上添加或删除 HTML 代码

javascript - Ajax请求可以读取excel表格吗

php - Jquery,读取从 PHP 接收的 JSON 变量