Three important attributes of the XMLHttpRequest object:
Properties |
Description |
onReadyStateChange |
The function (or function name) is called whenever the ReadyState property is changed. |
ReadyState |
The state of being xmlhttprequest. Vary from 0 to 4.
- 0: Request not initialized
- 1: Server Connection established
- 2: Request received
- 3: In Request processing
- 4: The request is complete and the response is ready
|
Status |
$: "OK" 404: Page Not Found |
Example:
1 xmlhttp.onreadystatechange=function () 2 {3 if (xmlhttp.readystate==4 && xmlhttp.status==200)4 {5 document.getElementById ("mydiv"). Innerhtml=xmlhttp.responsetext; 6 }7 }
When ReadyState equals 4 and the status is 200, the response is ready (note: The onReadyStateChange event is triggered 5 times (0-4), corresponding to each change of the readyState. )
Using the Callback function
The callback function is a function that is passed as an argument to another function.
Example:
1 <Scripttype= "Text/javascript">2 varXMLHTTP;3 functionLoadxmldoc (url,cfunc)/ * Pass the parameter, the first parameter represents the URL address, and the second parameter represents the function to execute * /4 {5 if(window. XMLHttpRequest)6 {//code for ie7+, Firefox, Chrome, Opera, Safari7 XMLHTTP=NewXMLHttpRequest ();8 }9 ElseTen {//code for IE6, IE5 One XMLHTTP=NewActiveXObject ("Microsoft.XMLHTTP"); A } - Xmlhttp.onreadystatechange=Cfunc; / * Call function * / - Xmlhttp.open ("GET", URLs,true); the xmlhttp.send (); - } - functionmyFunction () - { + Loadxmldoc ("/ajax/test1.txt",function() - { + if(Xmlhttp.readystate==4 &&Xmlhttp.status== $) A { at document.getElementById ("mydiv"). InnerHTML=Xmlhttp.responsetext; - } - }); - } - </Script>
Ajax-onreadystatechange Event (properties of the XMLHttpRequest object)