admin管理员组文章数量:1403516
In book JavaScript: The Definitive Guide, 5th Edition by David Flanagan, says: before sending AJAX request, you have to send request headers.
In a scope of cross browsers support, do I need to do that?
request.setRequestHeader("User-Agent", "XMLHttpRequest");
request.setRequestHeader("Accept-Language", "en");
request.setRequestHeader("If-Modified-Since", lastRequestTime.toString());
In book JavaScript: The Definitive Guide, 5th Edition by David Flanagan, says: before sending AJAX request, you have to send request headers.
In a scope of cross browsers support, do I need to do that?
request.setRequestHeader("User-Agent", "XMLHttpRequest");
request.setRequestHeader("Accept-Language", "en");
request.setRequestHeader("If-Modified-Since", lastRequestTime.toString());
Share
Improve this question
asked Jul 8, 2011 at 23:24
Dexy_WolfDexy_Wolf
9993 gold badges13 silver badges26 bronze badges
2 Answers
Reset to default 3For the last 2 header, they are not mandatory at all for browser patibility. Those header are used as preference indication (Accept-Language) and content optimization (If-Modified-Since).
request.setRequestHeader("Accept-Language", "en");
request.setRequestHeader("If-Modified-Since", lastRequestTime.toString());
The first header is used on the server side to detect whether a query was done from AJAX or simply navigation. Older browser may not set this header by default, so you can lose browser patibility if your server relies on this header to be set. If your server doesn't relies on this header to be set, you won't lose any browser patibility if it's not set.
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
Note that the first header should be X-Requested-With
and not User-Agent
.
Look to jQuery for guidance:
http://code.jquery./jquery-latest.js
Specifically search for "X-Requested-With". You don't need to set the "User-Agent". You may want to set the "X-Requested-With" to "XMLHttpRequest", though.
Seems to be a lot of questions from people trying to reimplement jQuery without looking at the source of either jQuery or DoJo or MooTools or any other JS framework. Use the source, Luke. Or just use the framework and build a useful app instead of re-solving solved problems as an academic exercise.
Check out this series of blog posts from dailyjs where they build a framework from scratch:
http://dailyjs./tags.html#lmaf
本文标签: javascriptSending AJAX request with headersStack Overflow
版权声明:本文标题:javascript - Sending AJAX request with headers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744421238a2605464.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论