XMLHttpRequest is?
The user exchanges data with the server in the background.
You can update the Web page without reloading the page;
Request data from the server after the page has been loaded;
Receiving data from the server after the page has been loaded;
Sends data to the server in the background.
All modern browsers support XMLHttpRequest objects.
Create XMLHttpRequest
<script text= "Text/script" >varXMLHTTP;functionloadxmldoc (URL) {XMLHTTP=NULL;if(window. XMLHttpRequest) {//code for all new browsersxmlhttp=NewXMLHttpRequest (); }Else if(window. ActiveXObject) {//code for IE5 and IE6xmlhttp=NewActiveXObject ("Microsoft.XMLHTTP"); }if(xmlhttp!=NULL) {Xmlhttp.onreadystatechange=State_change; Xmlhttp.open ("GET", URL,true); Xmlhttp.send (NULL); }Else{alert ("Your Browser does not support XMLHTTP."); }}functionState_change () {if(xmlhttp.readystate==4) {//4 = "Loaded" if(xmlhttp.status==200) {//K = OK //... our code here ... } Else{alert ("Problem retrieving XML data"); } }}</script>
Note: onReadyStateChange is an event handle. Its value (state_change) is the name of a function that fires when the state of the XMLHttpRequest object changes. The status changes from 0 (uninitialized) to 4 (complete). We execute the code only if the status is 4 o'clock.
Why use Async=true?
Our instance uses "true" in the third argument of open ().
This parameter specifies whether the request is processed asynchronously.
True indicates that the script will continue to execute after the Send () method without waiting for a response from the server.
The onReadyStateChange event complicates the code. But this is the safest way to prevent code from stopping without getting a response from the server.
By setting this parameter to "false", you can omit the extra onreadystatechange code. This parameter can be used if the execution of the remaining code does not matter if the request fails.
Receive text
Xmlhttp.responsetext;
Convert the custom text to XML
if (window. Domparser) { new domparser (); = parser.pasefromstring (txt, "text/xml");} Else { new ActiveXObject ("Microsoft.XMLDOM"); false ; Xmldoc.loadxml (TXT);}
Receive XML
Xmlhttp.responseXML.documentElement.getElementsByTagName ("");
Request Head
Xmlhttp.getallresponseheaders ();
Request to specify head
Xmlhttp.getresponseheader ("");
Is the XMLHttpRequest object a standard for the audience?
XMLHttpRequest objects are not specified in any recommended standards.
However, the "Load and Save" specification of the Web DOM level 3 contains some similar functionality, but no browsers have implemented them.
XMLHttpRequest Object Usage