JavaScript for AJAX Encapsulation

Source: Internet
Author: User

1 Pure JavaScript encapsulation sends AJAX data

2 post, get mode to send data

1Global.namespace ("Ajax");2 /**3 * Synchronous Ajax returns JSON Object4  * 5 * @param {}6 * Urlstr7 * @param {} type GET or post mode8 * @param {}9 * PARAMSSTR and Server conventions if the Get method must be Key1 = value & key2 = valueTen * The form is added after the URL One * @return {} return string A  */ -GLOBAL. Ajax.ajaxsynccall =function(Urlstr, type, paramsstr) { -     varobj; the     varvalue; -     if(window. ActiveXObject) { -obj =NewActiveXObject (' Microsoft.XMLHTTP '); -}Else if(window. XMLHttpRequest) { +obj =NewXMLHttpRequest (); -     } +     if(Type = = "POST") { AObj.open (' POST ', URLSTR,false); atObj.setrequestheader (' Content-type ', -' Application/x-www-form-urlencoded '); - obj.send (PARAMSSTR); -}Else if(Type = = "GET") -     { -Obj.open (' GET ', URLSTR,false); inObj.send (NULL); -     } to     varresult =NULL; +     if(Obj.readystate = = 4) { -         if(Obj.status = = 200) { theresult =Obj.responsetext; *         } $     }Panax Notoginseng     returnresult; - } the /*Asynchronous transfer method for Ajax*/ +GLOBAL. Ajax.ajaxasyncallback =function(XHR, callback) { A     varresult =NULL; the     if(Obj.readystate = = 4) { +         if(Obj.status = = 200) { -result =Obj.responsetext; $ callback (result); $         } -     } - } the /** - * Ajax Asynchronous Submission MethodWuyi  *  the * @param {} - * Urlstr Wu * @param {} - * GET or Post mode About * @param {} $ * Paramjsonobj - * @param {} - * Callbackfunc -  */ AGLOBAL. Ajax.ajaxasyncall =function(Urlstr, type, PARAMSSTR, callback) { +     varobj; the     varvalue; -     if(window. ActiveXObject) { $obj =NewActiveXObject (' Microsoft.XMLHTTP '); the}Else if(window. XMLHttpRequest) { theobj =NewXMLHttpRequest (); the     } theObj.onreadystatechange =GLOBAL. Ajax.ajaxasyncallback (obj, callback); -     if(Type = = "POST") { inObj.open (' POST ', URLSTR,true); theObj.setrequestheader (' Content-type ', the' Application/x-www-form-urlencoded '); About obj.send (PARAMSSTR); the}Else if(Type = = "GET") the     { theObj.open (' GET ', URLSTR,true); +Obj.send (NULL); -     } the}

The parameter method that needs to be used, in the base class:

1 Creating a Post string

2 Creating a Get string

1 /*2 * @parameters as an object3 * Returns a URL string with a value4 */5GLOBAL. tool.creatgetparam=function(url,parameters)6 {7    varRtnstr=NULL;8    varTmpstr= ", str;9    vari = 0;TenRtnstr = url+ "?"; One     for(varKeyinchparameters) { Astr = Escape (key) + "=" +Escape (Parameters[key]); -         if(i==0) -         { thetmpstr+=str; -i++; -         } -         Else{ +tmpstr+= ' & ' +str; -         } +    } Artnstr+=tmpstr; at    returnRtnstr; - } -  -GLOBAL. tool.cratepostparam=function(Parameters) - { -     varRtnstr = ' '; in     vari = 0; -     varTmpstr= ", str; to      for(varKeyinchparameters) +     { -str = Escape (key) + "=" +Escape (Parameters[key]); the         if(i==0) *             { $tmpstr+=str;Panax Notoginsengi++; -             } the         Else  +             { Atmpstr+= ' & ' +str; the             } +     } -     returntmpstr; $}

JavaScript for AJAX Encapsulation

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.