Myajax는 JS로 작성된 크로스 브라우저 Ajax 라이브러리로 Get, Post, JSONP 요청, 절묘하고 간단한 것을 지원합니다.
1. GET 요청 보내기 :
myajax.get ({<span style = "white style : pre"> </span> data : {}, // parameter url : ", // 요청 주소 // // 오류가 발생하는 오류가 호출됩니다 : 기능 (data) {}, // 성공적으로 성공적으로 요청 : 기능 (data) {<span style ="white ">/span to anciver to json});2. 게시물 요청 보내기 :
myajax.post ({data : {}, // parameter url : "", //// 오류가 발생한 오류가 호출 오류 : 함수 (data) {}, // 성공적으로 호출 된 요청 : 함수 (data) {// eval); string을 JSON}});3. JSONP 요청 보내기 :
myajax.getJsonp ({// 매개 변수 데이터 : {}, url : "", // 요청 주소 // 성공적으로 요청 : function (data) {}, // 호출 오류 : function () {}} 오류가 발생하면;소스 코드 :
var myajax = {post : function (params) {var xmlhttp = this.createxmlhttprequest (); if (xmlhttp! = null) {var async = true; if (typeof params.async! = "undefined") async = params.async; var (typeof param. params.data; var url = ""; if (typeof params.url! = "undefined") url = params.url; if (url == null || url || url.length == 0) return; xmlhttp.open ( "post", url, async); if (async) {xmlhttp.on intecide () {if ready change () (this.readystate == 4) {if (this.status == 200) {if (typeof params.success! = "undefined") {params.success (xmlhttp.responsext);}} else {if (typeof params.error! = "nisfined") XMLHTTP.STATUSTEXT);} console.error (url + ":" + xmlhttp.status);}}};} xmlhttp.setRequestHeader ( "content-type", "application/x-ww-form-urlencoded"; var prop in (var prop in data) "; 데이터 [prop] + "&";} param = param.substring (0, param.length -1); xmlhttp.send (param); if (! async) {if (xmlhttp.readystate == 4 && xmlhttp.status == 200) if (type typeparams.success! {params.success (xmlhttp.responsext);} else {if (typeof params.error! = "undefined") {params.error (xmlhttp.status + xmlhttp.stateStext);} console.error (url + ":" + xmlhttp. gets) function (params) {var xmlhttp = this.createxmlhttprequest (); if (xmlhttp! = null) {var async = true; if (params.async! = undefined) async = params.async; var url = ""; if (url | url.length == 0) return; if (params.data! = null) {var data = params.data; var paramprefix = url.indexof ( "?") == -1? "?" : "&"; url = url + paramprefix; for (data in data in data) {url + = prop + "=" + data [prop] + "&";} url = url.substring (0, url.length -1);} xmlhttp.open ( "get", url, async); if (async) {xmlhtttp. function () {if (if (this.readeystate == 4) {if (this.status == 200) {if (typeof params.success! = "undefined") {params.success (xmlhttp.responsetext);}} else {if (typeof params.error! = "undefined") xmlhttp.statustext);} console.error (url + ":" + xmlhttp.status);}}};} xmlhttp.send (null); if (! async) {if (xmlhttp.readystate = 4 && xmlhtp.status == 200). params.success! = "undefined") {params.success (xmlhttp.responsext);} else {if (typeof params.error! = "undefined") {params.error (xmlhttp.status + xmlhttp.staturext); xmlhttp.status);}}}}, createxmlhttprequest : function () {if (window.xmlhttprequest) {return new new xmlhttprequest ();} else if (window.activexobject) {// ie5 및 ie6preturn에 대한 코드 ActiveXObject ( "microsoft.xmlhttp");} return null;}, getJsonp : function (params) {var url = null; if (typeof params.url! = "undefined") {url = params.url;} if (url == null) {return;} var ff = " + new 날짜 (). (parseint (math.random () * 10000000000)); eval ( "jsonpcallback_" + ff + "=" + function (data) {if (typeof params.success! = "undefined") {params.success (data);}}); // "?" "?"에 따라 타임 스탬프 매개 변수를 추가 할 때 URL에 나타납니다. 또는 "&"var paramprefix = url.indexof ( "?") == -1? "?" : "&"; url = url + paramprefix + "jsonpcallback =" + "jsonpcallback_" + ff; var param = "; if (typeof params.data! ="undefined "&& params.data! = null) {var data = params.data; for (var prop in data) +" + data + " + data [prop] +" param.substring (0, param.length -1);} if (param.length> 0) url = url + "&" + param; var script = document.createElement ( "스크립트"); document.body.appendChild (스크립트); script.src = url; script.charset = "utf-8"; // firefox, google 등, google etcror = function () {if (typeof params.error! = "undefined") {params.error ()}} script.onload = function () {document.body.removeChild (script); } // IE script.onreadyStateChange = function () {if (this.readystate = "roaded"|| this. readystate = "{document.body.removechild (script); }}}};JS로 작성된 위의 Ajax 라이브러리 (예제 코드)는 내가 공유하는 모든 내용입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.