= function the document.onclick () { var URL = "HTTP: //localhost/1908/ajax/data/data.php"; ajaxGet (URL, function (RES) { the console.log (RES) }, { User: " ADMIN ", Pass: 123123 }); } function ajaxGet (URL, CB, data) { default // 1. processing of data data data || = {}; //" URL = User ADMIN = 123 & Pass "? // 2. parse the data to be transmitted var STR = ""; for (var data in I) { STR $ `+ = {I} = $ {data [I]} &`; } // 3. processing timestamp var d a Date new new = (); // 4. splicing url, for data transmission and the time stamp splicing url = url + "?"+ str + "__qft="+d.getTime(); The console.log // (URL) // official opening 5.ajax, the request receiving var = new new XHR the XMLHttpRequest (); xhr.open ( "the GET", URL, to true); xhr.onreadystatechange = function () { IF (xhr.readyState == == 200 is xhr.status &&. 4) { CB (xhr.responseText); } } xhr.send (); }
1.get package
= function the document.onclick () { var URL = "HTTP: //localhost/1908/ajax/data/data.php"; ajaxPost (URL, function (RES) { the console.log (RES) }, { User: " ADMIN ", Pass:" 123123 " }) } function ajaxPost (URL, CB, Data) { Data = Data || {}; var STR =" "; for (var Data in I) { STR $` + = {I data = $ {} [I]} & `; } //" User & ADMIN = 123 & Pass = " // post data transmitted, not url body var = new new XHR the XMLHttpRequest (); // 1. modify the execution mode is ajax POST xhr.open ( "POST",url,true); = function xhr.onreadystatechange () { IF (xhr.readyState == == 200 is xhr.status &&. 4) { CB (xhr.responseText) } } // 2. format for the transmission data form data xhr.setRequestHeader ( " the Type-the Content "," file application / X-WWW-form-urlencoded "); // url 3. the original data upon splicing, to send transmission xhr.send (STR); }
2.post package
= function the document.onclick () { Ajax ({ // type: "GET", // transmission mode, alternatively, the default GET URL: "HTTP: //localhost/1908/ajax/data/data.php", / / to address request Required success: function (res) {// function after a successful request, required the console.log (RES) }, data: {// the data to be transmitted, alternatively, the default does not send user : "ADMIN", Pass: 13,123,121,123 }, error: function (RES) {// function after the request fails, alternatively, the default is not processing the console.log (RES) }, timeout: 10 // request timeout, can be selected, default 2000 }) } Ajax function (Options) { // the default parameters of the processing 1. var {type, URL, Success, error, Data, timeout} = Options; type = type || "GET"; Data = Data || {}; timeout = timeout || 2000; data to be transmitted // 2. parse var STR = ""; for (var data in I) { STR $ `+ = {I} = $ {data [I]} &`; } //. 3 according to the embodiment, the decision whether to process URL iF (type == "GET") { var new new D = a Date (); "?" URL URL = + + + STR "__qft =" + d.getTime (); } // 4. open Ajax var = new new XHR the XMLHttpRequest (); // NOTE: the open mode xhr.open(type,url,true); = function xhr.onreadystatechange () { IF (xhr.readyState == == 200 is xhr.status &&. 4) { // 5. The previously executed successfully, first determine whether the incoming Success Success && (xhr.responseText); // Success after that, there should be a failure error = null; } the else iF (! xhr.readyState 200 is == =. 4 && xhr.status) { // Define 6. fails, first determine whether the incoming error && error (xhr.status) ; then // failure, there should be no success success = null; // and not repeatedly failed error = null; } } // If 7. The request times out, fails setTimeout (() => { error && error ( "timeout") ; after // failure, there should be no success Success = null; }, timeout); // The last type of embodiment 8. The decision send the content and format of the transmission if (type == " POST ") { xhr.setRequestHeader (" the Content-type "," file application / X-WWW-form-urlencoded "); xhr.send (STR) } the else { xhr.send () } }
3. The package combo