在我的应用中,我正在创建从HTTP到HTTPS的AJAX请求。这意味着我需要CORS。因此,我向jQuery.ajax添加了一些标题和参数并对其进行了测试。在Firefox中,一切正常,但在Chrome中,一切正常。Chrome会“杀死”每一个预先提出的请求(选项)。
jQuery脚本:
$(document).on('click', 'a.ajax', function(e) { e.preventDefault(); $.ajax(this.href, { type: 'GET', dataType: 'json', crossDomain: false, headers: {'X-Requested-With': 'XMLHttpRequest'}, xhrFields: { withCredentials: true } }); return false; });
HTTP转储:
> OPTIONS /foo HTTP/1.1 > User-Agent: curl/7.29.0 > Host: local.bar.cz > Accept: */* > Access-Control-Request-Headers:accept, origin, x-requested-with > Access-Control-Request-Method:GET > Origin:http://local.bar.cz > < HTTP/1.1 204 < Server: nginx/1.2.7 < Date: Wed, 27 Feb 2013 15:06:54 GMT < Content-Type: text/html; charset=utf-8 < Connection: keep-alive < X-Powered-By: Nette Framework < X-Frame-Options: SAMEORIGIN < Access-Control-Allow-Origin: http://local.bar.cz < Access-Control-Allow-Credentials: true < Access-Control-Allow-Headers: accept, origin, x-requested-with < Access-Control-Allow-Methods: OPTIONS, GET, POST, HEAD <
有人知道为什么Chrome会终止此请求吗?
也许您的https服务器具有不受信任的证书。如果是这样,请尝试首先使用浏览器访问该URL,然后接受不受信任的连接。