Useful AJAX code sharing

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.