Ajax, is the asynchronous request, specifically want to update a part of the page, no need to reload the entire page, as long as the sending request to update the local page.
Ajax, not new language, just a technology
The use of Ajax relies on XMLHttpRequest objects, and the "members" commonly used in objects include: Open, Send,responsetext,onreadystatechange
Create Xhr object: var xmlrequest = new XMLHttpRequest ();
xhr Send request: Xmlrequest.open ("method", "url", True/false);
Xmlrequest.send ([string]);
parameters in open:
- Method:get/post, and select one to indicate the method used when sending
- URL: The location of the file on the server, or the path
- The third argument, if true, is asynchronous, and vice versa is synchronous
parameters in Send:
1. String can only be used when the POST request, the GET request to transfer more content to add in the URL
XHR Response: When transmitting a string, use: responsetext eg:var tmptext = Xmlrequest.responsetext;
The transport XML format is, using: responsexml eg:var tmpxml = xmlrequest.responsexml;
onReadyStateChange: Use this to store the function to be executed when the data is returned; Here are two volumes: one is readyState and the other is status. Whenever the value of readystate changes, it is called
onReadyStateChange this function.
value of readystate:0-Request not initialized
The server connection has been established
2-Request received
3-In Request processing
4-The request is complete and the response is ready
Status value:"404": Page not Found
"$": OK
Examples from the world-class:
Xmlhttp.onreadystatechange=function () { if (xmlhttp.readystatexmlhttp.status==200) { document.getElementById ("mydiv"). innerhtml=xmlhttp.responsetext; } }
Use a callback function instead of onreadystatechange, written like this:
function Loadxmldoc () {.... AJAX script goes here ...} </script>
function MyFunction () { Loadxmldoc ("Ajax_info.txt", function () { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getElementById ("mydiv"). Innerhtml=xmlhttp.responsetext; } );}
Ajax JavaScript Learning Summary