Ajax. js
------------------------- [Ajax class] --------------------------
Copy codeThe Code is as follows:
Function Ajax (recvType ){
Var aj = new Object ();
Aj. recvType = recvType? RecvType. toUpperCase (): 'html'; // file type passed to the parameter
Aj.tar getUrl = '';
Aj. sendString = '';
Aj. resultHandle = null;
/* Create an XMLHttpRequest object */
Aj. createXMLHttpRequest = function (){
Var xmlHttp = false;
If (window. XMLHttpRequest) {// create an XMLHttpRequest object in non-IE
XmlHttp = new XMLHttpRequest ();
} Else if (window. ActiveXObject ){
Try {
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP"); // create by new IE version
} Catch (error1) {// creation failed
Try {
XmlHttp = new ActiveXobject ("Microsoft. XMLHttp"); // create by IE
} Catch (error2) {// creation failed
XmlHttp = false;
}
}
}
Return xmlHttp;
}
Aj. XMLHttpRequest = aj. createXMLHttpRequest ();
/* Process the server response */
Aj. processHandle = function (){
If (aj. XMLHttpRequest. readyState = 4 ){
If (aj. XMLHttpRequest. status = 200 ){
If (aj. recvType = "HTML ")
Aj. resultHandle (aj. XMLHttpRequest. responseText );
Else if (aj. recvType = "XML ")
Aj. resultHandle (aj. XMLHttpRequest. responseXML );
}
}
}
/* Define the method to be passed using the get Method */
Aj. get = function (targetUrl, resultHandle ){
Aj.tar getUrl = targetUrl;
If (resultHandle! = Null ){
Aj. XMLHttpRequest. onreadystatechange = aj. processHandle;
Aj. resultHandle = resultHandle;
}
If (window. XMLHttpRequest ){
Aj. XMLHttpRequest. open ("get", aj.tar getUrl );
Aj. XMLHttpRequest. send (null );
} Else {
Aj. XMLHttpRequest. open ("get", aj.tar getUrl, true );
Aj. XMLHttpRequest. send ();
}
}
/* Define the method to be passed using the post method */
Aj. post = function (targetUrl, sendString, resultHandle ){
Aj.tar getUrl = targetUrl;
If (typeof (sendString) = "object "){
Var str = "";
For (var pro in sendString ){
Str + = pro + "=" + sendString [pro] + "&";
}
Aj. sendString = str. substr (0, str. length-1 );
} Else {
Aj. sendString = sendString;
}
If (resultHandle! = Null ){
Aj. XMLHttpRequest. onreadystatechange = aj. processHandle;
Aj. resultHandle = resultHandle;
}
Aj. XMLHttpRequest. open ("post", targetUrl );
Aj. XMLHttpRequest. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
Aj. XMLHttpRequest. send (aj. sendString );
}
Return aj;
}
------------------------- [Usage] --------------------------
UseAjax.html
Copy codeThe Code is as follows:
<Script src = "ajax. js"> </script>
<Script language = "javascript" type = "text/javascript">
Var ajax = Ajax ();
/* Get usage */
Ajax. get ("server. php? Name = zhangsan & phone = 778 ", function (data ){
Alert (data); // data is the data read from the server
});
/* First post usage */
/* Ajax. post ("server. php", "name = ligang & phone = 222", function (data ){
Alert (data );
});
*/
/* Second post usage */
/* Ajax. post ("server. php", {name: "tom", phone: "456"}, function (data ){
Alert (data );
});
*/
</Script>
Server. php
<? Php
Header ("Content-type: text/html; charset = gb2312 ");
$ Str = "name: {$ _ GET [" name "]} \ n Tel: {$ _ GET [" phone "]}";
Echo $ str;
?>
Enter the useajax.html address in the browser. If
The Ajax method is used correctly.