Instance code:
Encapsulated AJAX Network Request function//obj is an object function AJAX (obj) {//Cross-domain request if (Obj.datatype = "Jsonp") {//here callback must be It's a global variable. The variable cannot be destroyed//processed when the function name (prevents multiple network request functions from having the same name disorder) var hehe = "CallBack" + "_" + New Date (). Gettim
E () + "_" + String (Math.random ()). Replace (".", "");
Window[hehe] = obj.success;
Create a script tag var sc = document.createelement ("script");
SC.SRC = Obj.url + "?" + "cb=" + hehe;
Console.log (SC.SRC);
Document.body.appendChild (SC);
Document.body.removeChild (SC);
Return
{///1, create Ajax object var ajaxobj = null; if (window.
XMLHttpRequest) {ajaxobj = new XMLHttpRequest ();
}else{ajaxobj = new ActiveXObject ("Microsoft.XMLHTTP"); //Set the type of request Obj.type = Obj.type.toUpperCase () | |
"Get"; If it is a GET request and you need to pass the parameter, you need to give the URL a concatenation parameter if (Obj.type = = "Get") {var arr = [];//definition array is used to store the object in the data for the (Var key in OB
J.data) {Arr.push (key + "=" + Obj.data[key]); ///With & spacer array Let it be transformed into something like this: the name=lxl&age=18 form var str = arr.join ("&"); Obj.url = Obj.url + "?"
+ str;
Dial-up Ajaxobj.open (obj.type,obj.url,true);
Send "Name=123&age=18" ajaxobj.send ();
}else{var arr = [];//definition array is used to store objects into the data for (Var key in Obj.data) {Arr.push (key + "=" + Obj.data[key]);
Console.log (arr);
///Use & spacer array to convert it to resemble: name=lxl&age=18 form var str = arr.join ("&");
Console.log (str);
Ajaxobj.open (obj.type,obj.url,true);
Ajaxobj.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Ajaxobj.send (str); }//Listener Ajaxobj.onreadystatechange = function () {if (ajaxobj.readystate = 4) {if (Ajaxobj.status >= && Ajaxobj.status < 300 | |
Ajaxobj.status = = 304) {//Request successful obj.success (Ajaxobj.responsetext);
}else{//Request Failure Obj.error (ajaxobj.status);
}
}
}
}
Above the AJAX network request for the package example is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.