Ajax can be loaded asynchronously, interacting with the server and updating one part of the page without refreshing the entire page. There are two main ways to request it: Get and post.
Because XMLHttpRequest is supported by more than IE6, it is best to declare the object as follows in this way:
if (window. XMLHttpRequest) { var xhr = new XMLHttpRequest ();} else{ var xhr = new ActiveXObject (' Microsoft.XMLHTTP ');}
To connect to the server after creating the object:
Xhr.open (' GET ', ' Server-side code ' address? keyword=c&user_name=mike&age=20 ', true);
Then send the request:
Xhr.send ();
Then accept the return value:
Xhr.onreadystatechange=function () { if (xhr.readystate==4) { if (xhr.status==200) { alert ( Xhr.responsetext); } else{ alert ("Failed")}} ;
If you use the Set method then is the above notation, the passed parameters are placed in the URL, multiple parameters with & connection.
If this is the Post method:
Xhr.open ("POST", "ajax.php",true); Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Xhr.send ("key1=x&key2=q&key3=w"); Xhr.onreadystatechange=function() { if(xhr.readystate==4&& xhr.status==200) { alert (xhr.responsetext); } }
Above the service side of the code in ajax.php, with the Post method needs to add setRequestHeader method, the wording is shown above. The parameters to be passed are placed inside the Send method.
Get method: Fast, pass the data size is only 4KB, suitable for the transfer of small amount of data, the passed parameters will be exposed in the URL.
Post method: Almost unlimited capacity, in most cases for uploading data.
The Ajax of JavaScript