Myajax ist eine Cross-Browser-Ajax-Bibliothek, die in JS geschrieben wurde, die Get, Post, JSONP-Anfragen, exquisit und einfach unterstützt.
1. Senden Sie eine Get -Anfrage:
myajax.get ({<span style = "White-Space: PRE"> </span> Daten: {}, // Parameter url: "", // Anfordernadresse // Der Fehler ist aufgetretener Fehler: Funktion (Daten) {}, // erfolgreich als Erfolg bezeichnet: Funktion (Data) {<span Style = "White-Space: Pre"}}}}}}} ·) </>/span>/);2. Postanfrage senden:
myajax.post ({Daten: {}, // Parameter URL: "", //// Der Fehler ist aufgerufener Fehler: Funktion (Daten) {}, // Die Anforderung, erfolgreich als Erfolg bezeichnet: Funktion (Daten) {// eval (Daten);3. Senden Sie JSONP -Anfrage:
myajax.getJsonp ({// Parameterdaten: {}, url: "", // Anfrageadresse // Anfordern erfolgreich den Erfolg aufrufen: Funktion (Daten) {}, // Call -Fehler: Funktion () {}} Wenn ein Fehler auftritt;Quellcode:
var myajax = {post: function (params) {var xmlhttp = this.createxmlhttprequest (); if (xmlhttp! params.data; var url = ""; if (typeof params.Url! (this.ReadyState == 4) {if (this.status == 200) {if (typeof params.success! xmlhttp.StatUntext);} console.Error (url + ":" + xmlhttp.status);}}};} xmlhttp.setRequestheader ("content-type", "Application/x-www-form-urlencoded"; Data [prop] + "&";} param = param.substring (0, param.length - 1); xmlhttp.send (param); if (! async) {if (xmlhttp.readystate == 4 && xmlhttp.status == 200) if (typeof params! {params.success (xmlhttp.responSeText);} else {if (TypeOf params.Error! Funktion (params) {var xmlhttp = this.createxmlHttprequest (); if (xmlhttp! url.length == 0) return; if (params.data! = null) {var data = params.data; var paramprefix = url.indexof ("?") == -1? "?" : "&"; url = url + paramprefix; für (var prop in data) {url + = prop + "=" + data [prop] + "&";} url = url.substring (0, url.Length - 1);} xmlhttp.open ("get", url, async); function () {if (this.ReadyState == 4) {if (this.status == 200) {if (typeof params.success! xmlhttp.statusText);}console.error(url + ": " + xmlhttp.status);}}};}xmlhttp.send(null);if (!async) {if (xmlhttp.readyState == 4 && xmlhttp.status == 200)if (typeof params.success! xmlhttp.status);}}}}, createxmlhttpRequest: function () {if (window.xmlhttprequest) {Neue xmlhttprequest ();} else if.activeXObject {// code für IE5 und IE6RETURNNEN NEWE ActiveXObject ("microsoft.xmlhttp");} return null;}, getJsonp: function (params) {var url = null; if (typeof params.Url! = "Undefined") {url = params.Url; (parseInt (math.random () * 1000000000)); eval ("jsonpcallback_" + ff + "=" + function (data) {if (typeof params.success! Beim Hinzufügen von Zeitstempelparametern basierend darauf, ob "?" erscheint in der URL. oder "&" var paramprefix = url.indexof ("?") == -1? "?" : "&"; url = url + paramprefix + "jsonpcallback =" + "jsonpcallback_" + ff; var param = ""; if (typeof params.data! = "undefined" && params.data! param.substring (0, param.length - 1);} if (param.length> 0) url = url + "&" + param; var script = document.createelement ("script"); document.body.appendchild (script); script.src = url; script.charset = "utf-8"; // für Firefox, Google usw.Script.Onerror = function () {if (TypeOf params.Error! } // für IE script.onReadyStatechange = function () {if (this.readyState == "geladen" || this.readyState == "complete") {document.body.removechild (script); }}}};Der in JS geschriebene Ajax Library (Beispielcode) ist der gesamte Inhalt, den ich mit Ihnen teile. Ich hoffe, Sie können Ihnen eine Referenz geben und ich hoffe, Sie können wulin.com mehr unterstützen.