jQuery的AJAX无法工作(OPTIONS pre-飞行要求订做)指定头时,
AJAX请求工作正常,但现在我通过beforeSend或标题添加一个头,一个OPTIONS pre-航班提出要求,并GET请求被中止。
The AJAX request works fine, but the moment I add a header via beforeSend or headers, an OPTIONS pre-flight request is made and the GET request is aborted.
Code: $.ajax({
type: "GET",
crossDomain: true,
beforeSend: function (xhr)
{
xhr.setRequestHeader("session", $auth);
},
url: $url,
success: function (data) {
$('#something').html(data);
},
error: function (request, error) {
$('#something').html("<p>Error getting values</p>");
}
});
类似 AJAX请求W / O指定表头(现在我添加/修改标题,一个OPTIONS调用时)
Similar AJAX Request w/o headers specified (the moment I add/modify header, an OPTIONS call is made)
Request GET /api/something?filter=1 HTTP/1.1
Referer http://app.xyz.dj/dashboard
Accept application/json, text/javascript, */*; q=0.01
Accept-Language en-US
Origin http://app.xyz.dj
Accept-Encoding gzip, deflate
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; MASMJS; rv:11.0) like Gecko
Host 162.243.13.172:8080
DNT 1
Connection Keep-Alive
Cache-Control no-cache
类似的服务器响应头(用于GET请求)
Similar Server Response Header (for GET request)
Response HTTP/1.1 200 OK
Server Apache-Coyote/1.1
Access-Control-Allow-Origin *
Access-Control-Allow-Methods GET, POST, DELETE, PUT, OPTIONS, HEAD
Access-Control-Allow-Headers Content-Type, Accept, X-Requested-With
Access-Control-Allow-Credentials true
Content-Type application/json
Transfer-Encoding chunked
Date Thu, 09 Jan 2014 14:43:07 GMT
我在做什么错了?
What I am doing wrong?
解决。 感谢@JasonP的指针。更改服务器的响应头从
Solved. Thanks @JasonP for pointers. Changed Server Response Headers from
访问控制 - 允许 - 标题:*
Access-Control-Allow-Headers:*
于特定的
访问控制 - 允许 - 头:内容类型,接受,X-要求-有了,会议
Access-Control-Allow-Headers: Content-Type, Accept, X-Requested-With, Session
和现在的作品!