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
Add a ment  | 

2 Answers 2

Reset to default 3

For 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