Why encapsulate Ajax, because ...
for (Var i=0;i<20;i++) {
$.ajax (...)
}
, the whole page was jammed, so I started looking for answers.
Later, found that jquery Ajax is the global, when the above is not done, the others will not move. As a package Ajax, Ajax can be used as part of the solution.
function Ajax (options) {options = Options | | {}; Options.type = (Options.type | | "GET"). toUpperCase (); Options.datatype = Options.datatype | | "JSON"; var params = Formatparams (Options.data); var xhr; The first step if (window. ActiveXObject) {//returns true for IE browser kernel, otherwise it is not IE kernel//the way to create a transfer object for IE kernel xhr = new window. ActiveXObject ("Microsoft.XMLHTTP"); } else {//How to create a Transfer object for a non-ie kernel browser xhr = new XMLHttpRequest (); }//Receive-third Step xhr.onreadystatechange = function () {if (xhr.readystate = = 4) {var status = Xhr.status; if (Status >= && status <) {options.success && options.success (x Hr.responsetext, Xhr.responsexml); } else { Options.error && options.error (status); }}}//Connect and send-second step if (Options.type = = "GET") { Xhr.open ("GET", Options.url + "?" + params, true); Xhr.send (NULL); } else if (Options.type = = "Post") {Xhr.open ("post", Options.url, True); Set the content type Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded") when the form is submitted; Xhr.send (params); }}//format parameter function formatparams (data) {var arr = []; for (var name in data) {Arr.push (encodeURIComponent (name) + "=" + encodeURIComponent (Data[name])); } Arr.push (("v=" + math.random ()). Replace (".", "")); Return Arr.join ("&"); }
The
also has a streamlined
function Ajax (opt) {opt = opt | | {}; Opt.method = opt.method.toUpperCase () | | ' POST '; Opt.url = Opt.url | | ‘‘; Opt.async = Opt.async | | True Opt.data = Opt.data | | Null opt.success = Opt.success | | function () {}; var xmlHttp = null; if (XMLHttpRequest) {xmlHttp = new XMLHttpRequest (); } else {xmlHttp = new ActiveXObject (' microsoft.xmlhttp '); }var params = []; for (var key in Opt.data) {Params.push (key + ' = ' + Opt.data[key]); } var postdata = Params.join (' & '); if (opt.method.toUpperCase () = = = ' POST ') {Xmlhttp.open (Opt.method, Opt.url, Opt.async); Xmlhttp.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded;charset=utf-8 '); Xmlhttp.send (PostData); } else if (opt.method.toUpperCase () = = = ' GET ') {Xmlhttp.open (Opt.method, Opt.url + '? ' + postdata, opT.async); Xmlhttp.send (NULL); } Xmlhttp.onreadystatechange = function () {if (xmlhttp.readystate = = 4 && Xmlhttp.status = = ) {opt.success (xmlhttp.responsetext); } }; }
But, if used in JQ, we can actually encapsulate this as well
function Ajax () {var ajaxdata = {Type:arguments[0].type | | "GET", Url:arguments[0].url | | "", Async:arguments[0].async | | " True ", Data:arguments[0].data | | NULL, Datatype:arguments[0].datatype | | "Text", Contenttype:arguments[0].contenttype | | "application/x-www-form-urlencoded", Beforesend:arguments[0].beforesend | | function () {}, success:arguments[0].success | | function () {}, Error:arguments[0].error | | function () {}} ajaxdata.beforesend () var xhr = Createxmlhttprequest (); Xhr.responsetype=ajaxdata.datatype; Xhr.open (Ajaxdata.type,ajaxdata.url,ajaxdata.async); Xhr.setrequestheader ("Content-type", Ajaxdata.contenttype); Xhr.send (Convertdata (ajaxdata.data)); Xhr.onreadystatechange = function () {if (xhr.readystate = = 4) {if (Xhr.status = =) {Ajaxdata.succe SS (Xhr.response)}else{ajaxdata.error ()}}}} function Createxmlhttprequest () {if (WI Ndow. ActiveXObject) {return New ActiveXObject ("Microsoft.XMLHTTP"); } else if (window. XMLHttpRequest) {return new XMLHttpRequest (); }} function Convertdata (data) {if (typeof data = = = ' object ') {var convertresult = ""; for (var c in data) {convertresult+= c + "=" + Data[c] + "&"; } convertresult=convertresult.substring (0,convertresult.length-1) return convertresult; }else{return data; } }
Calling the method is easy, so look at the parameters you need to know.
Encapsulates Ajax support for GET, post