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