在我的应用中,我正在从HTTP创建AJAX请求到HTTPS。这意味着我需要CORS。因此,我向jQuery.ajax添加了一些标头和参数并进行测试。在Firefox中一切正常,但在Chrome中不行。Chrome会“杀死”每个预检请求(OPTIONS)。
jQuery脚本:
任何人都知道为什么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终止了这个请求吗?
X-Requested-With
字段,将其设置为 ""。 - Rishi Diwan