Ajax Chrome:取消获取CORS HTTPS请求的请求
我尝试使用以下代码发出AJAX请求,从S3下载JSON文件:Ajax Chrome:取消获取CORS HTTPS请求的请求,ajax,json,google-chrome,amazon-s3,cors,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'); } }); 这
$.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
https://s3.amazonaws.com///document.json。不
“Access Control Allow Origin”标头出现在请求的服务器上
资源。来源“https://localhost.office.lucidpress.com”是
因此不允许访问
使用CURL发出相同的请求时,确实有以下标题:“访问控制允许来源:*”
为什么这对Chrome不起作用,有什么解决办法吗
注意:我在URL中添加了空格,以防止它们成为链接。Chrome将发送飞行前选项请求,以查看是否可以执行GET。如果你
curl-X选项-i,你会收到什么标题https://s3.amazonaws.com///document.json
?您很可能需要在服务器响应中添加Access Control Allow Headers
,以允许Chrome在选项请求中发送任何额外的头,即Access Control Allow Headers:Origin,X-request-With,Content Type,Accept
或者,由于这是一个https请求,您的答案可能是,看起来很可能就是它。S3对选项请求给出403。