XMLHttpRequest() (XHR for short) is an object (a constructor function) that allows you to send
HTTP requests from JavaScript. You can make an HTTP request to the server, get
the response, and update only a part of the page. This way you can build much more
responsive and desktop-like web pages.
varxhr=newXMLHttpRequest();xhr.onreadystatechange=myCallback;// The first parameter specifies the type of HTTP request (GET, POST, HEAD, and so on).// The second parameter is the URL you are requesting. // The last parameter is a boolean specifying whether the request is asynchronous// (true, always prefer this) or not (false, blocks all the JavaScript execution and// waits until the response arrives).xhr.open('GET','somefile.txt',true);// send() accepts any data you want to send with the request. // For GET requests, this is an empty string, because the data is in the URL. // For POST request, it's a query string in the form key=value&key2=value2.xhr.send('');// At this point, the request is sent and your code (and the user) can move on // The callback function myCallback will be invoked when the response comes// back from the server.
Processing the response
There is a property of the XHR object called readyState. Every time it changes, the readystatechange event fires.
readyState 属性的可取值:
0-uninitialized
1-loading
2-loaded
3-interactive
4-complete
1
2
3
4
5
6
7
8
9
10
11
functionmyCallback(){if(xhr.readyState<4){return;// not ready yet}if(xhr.status!==200){alert('Error!');// the HTTP status code is not OKreturn;}// all is fine, do the workalert(xhr.responseText);}
In Internet Explorer prior to version 7, the XMLHttpRequest object was an ActiveX
object, so creating an XHR instance is a little different. It goes like the following:
1
var xhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
创建 XMLHttpRequest 对象的兼容性代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
varids=['MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP'];varxhr;if(XMLHttpRequest){xhr=newXMLHttpRequest();}else{// IE: try to find an ActiveX object to usefor(vari=0;i<ids.length;i++){try{xhr=newActiveXObject(ids[i]);break;}catch(e){}}}