Creation of 1.xmlHttpRequest objects
Ajax core of the XMLHttpRequest object, the following code gives a compatible approach to each browser implementation
function createxhr () {if(typeofXMLHttpRequest! ="undefined"){ return NewXMLHttpRequest (); }Else if(typeofActiveXObject! ='undefined'){ if(typeofArguments.callee.activeXString! ="string"){ varversions=["msxml2.xmlhttp.6.0","msxml2.xmlhttp.3.0","msxml2.xmlhttp"]; varI,len; for(i=0; len=versions.length;i++){ Try{ NewActiveXObject (Versions[i]); Arguments.callee.activeXString=Versions[i]; Break; }Catch(ex) {}}} return NewActiveXObject (arguments.callee.activeXString); }Else{ Throw NewError ("no XHR object available ..."); }}
In fact, if you disregard the previous version of IE7, the use of native CHR can be achieved.
2,XHR Common methods
1) Xhr.open (): Accepts three parameters, request type (get,post), requested URL, whether it is an asynchronous request (is true, no is false)
2) Xhr.send (): Get is null,post for the data to be sent.
3) Xhr.setrequestheader (), set the request header
4) Xhr.onreadystatechage set the callback function of the asynchronous request, in order to ensure the compatibility of the browser, you need to specify the callback before open ()
The ReadyState property of the Xhr object changes during the request
1) 0: not initialized, the open () method has not been successfully invoked
2) 1: Start, already called open (), send not yet called ()
3) 2: Send, already called send ()
4) 3: Received, partial response has been received
5) 4: Complete, all response data has been received, the client can use
After the response is received, the response data is automatically populated with the following properties of the XHR object:
1) ResponseText: Response text
2) Responsexml If the response type is "Text/xml" or "Application/xml", the property has a value, otherwise it is empty
3) Status Response State code, general 200 is the flag of success, 304 is the data obtained from the cache
4) statustext response status Description, best not to use, in cross-browser not too reliable
JavaScript Learning Ajax