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(开发者_如何学Python"Accept-Language", "en");
request.setRequestHeader("If-Modified-Since", lastRequestTime.toString());
For the last 2 header, they are not mandatory at all for browser compatibility. 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 compatibility 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 compatibility 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.com/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.com/tags.html#lmaf
精彩评论