admin管理员组文章数量:1336632
I want to add header to my xhr.
When I use setRequestHeader, what it actually does is adding a value to Access-Control-Request-Headers.
If I code:
xhr.setRequestHeader('key1', 'value1');
xhr.setRequestHeader('key2', 'value2');
What I see in the request header is: Access-Control-Request-Headers:accept, key1, key2
What i expect to see instead is:
key1: value1
key2: value2
How can I prevent this merging? Thanks!
I want to add header to my xhr.
When I use setRequestHeader, what it actually does is adding a value to Access-Control-Request-Headers.
If I code:
xhr.setRequestHeader('key1', 'value1');
xhr.setRequestHeader('key2', 'value2');
What I see in the request header is: Access-Control-Request-Headers:accept, key1, key2
What i expect to see instead is:
key1: value1
key2: value2
How can I prevent this merging? Thanks!
Share Improve this question asked Apr 30, 2014 at 11:23 SRachamimSRachamim 9312 gold badges8 silver badges20 bronze badges1 Answer
Reset to default 7You're making a non-simple cross-domain XMLHttpRequest (read more about CORS here) which means that your browser must send an preflight (OPTIONS
) request prior to your intended request. This is done to verify with the server that the client from a different origin is allowed to make said request. When you are making a CORS request the browser will automatically add the Access-Control-Request
headers to the request when it gets sent.
The Access-Control-Request-Headers
header is a ma-delimited list of non-simple headers that are included in the request. The only "simple" headers you can set are: Accept
, Accept-Language
, Content-Language
, Last-Event-ID
, and Content-Type
(if it is set to one of: application/x-www-form-urlencoded
, multipart/form-data
, or text/plain
).
The server must respond to those Access-Control-Request
headers in the preflight request with the corresponding Access-Control-Allow
headers in its response. So in your case it would need to respond with Access-Control-Allow-Headers: key1, key2
.
本文标签: javascriptxhrsetRequestHeader() merging my headersStack Overflow
版权声明:本文标题:javascript - xhr.setRequestHeader() merging my headers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742412712a2470107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论