jQuery AJAX fails to work (OPTIONS pre-flight request made) when headers are specified(指定标头时,jQuery AJAX 无法工作(OPTIONS 飞行前请求))
问题描述
AJAX 请求工作正常,但是当我通过 beforeSend 或 headers 添加标头时,会发出一个 OPTIONS 飞行前请求并中止 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 请求不指定标头(当我添加/修改标头时,会进行 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
我做错了什么?
推荐答案
解决了.感谢@JasonP 的指点.将服务器 响应标头 更改为
Solved. Thanks @JasonP for pointers. Changed Server Response Headers from
访问控制允许标头:*
到特定的
Access-Control-Allow-Headers:Content-Type、Accept、X-Requested-With、Session
Access-Control-Allow-Headers: Content-Type, Accept, X-Requested-With, Session
现在它可以工作了!
这篇关于指定标头时,jQuery AJAX 无法工作(OPTIONS 飞行前请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:指定标头时,jQuery AJAX 无法工作(OPTIONS 飞行前请求)
- 400或500级别的HTTP响应 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01