An AJAX library (instance code) written in JS _javascript tips

Source: Internet
Author: User
Tags convert string to json string to json

Myajax is a cross-browser Ajax library written in JS, support get, post, JSONP request, compact, simple.

Send a GET request:

Myajax.get ({
<span style= "White-space:pre" >	</span>data: {},//Parameter
	URL: "",//Request address
	// The error occurred is calling
	error:function (data) {
	},
	//request successfully invoked
	Success:function (data) {		
<span style= " White-space:pre ">	</span>//eval (data); Convert string to JSON
	}
});

Second, send POST request:

Myajax.post ({
	data: {},//Parameter
	URL: ",/////
	error is calling
	error:function (data) {
	},
	//request successfully invoked
	success:function (data) {
	//eval (data); Convert string to JSON
	}
});

Third, send JSONP request:

Myajax.getjsonp ({
//parameter
data: {
},
URL: "",//Request address
//Request successfully invoke
success:function (data) {
////
Error:function () {}} When an error occurs
;

Source:

var Myajax = {Post:function (params) {var xmlhttp = this.createxmlhttprequest ();
			if (XMLHTTP!= null) {var async = true;
			if (typeof params.async!= "undefined") async = Params.async;
			var data = null;
			if (typeof params.data!= "undefined") data = Params.data;
			var url = "";
			if (typeof params.url!= "undefined") url = params.url;
			if (url = null | | url.length = 0) return;
			Xmlhttp.open ("POST", url, async);

							if (async) {xmlhttp.onreadystatechange = function () {if (this.readystate==4) {if (this.status==200) {
							if (typeof params.success!= "undefined") {params.success (xmlhttp.responsetext); } else {if (typeof params.error!= "undefined") {Params.error (xmlhttp.status + xmlhttp.stat
							Ustext);
						} console.error (URL + ":" + xmlhttp.status);
			}
					}
				};
			} xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); var param = "";
			For (var prop in data) {param + = prop + "=" + Data[prop] + "&";
			} param = param.substring (0, param.length-1);
			Xmlhttp.send (param); if (!async) {if (xmlhttp.readystate = = 4 && xmlhttp.status =) if (typeof params.success!=
					"undefined") {params.success (xmlhttp.responsetext);
					else {if (typeof params.error!= "undefined") {Params.error (xmlhttp.status + xmlhttp.statustext);
				} console.error (URL + ":" + xmlhttp.status);
		}}}, Get:function (params) {var xmlhttp = this.createxmlhttprequest ();
			if (XMLHTTP!= null) {var async = true;
			if (params.async!= undefined) async = Params.async;
			var url = "";
			if (params.url!= undefined) url = params.url;
			if (url = null | | url.length = 0) return;
				if (params.data!= null) {var data = Params.data; var paramprefix = Url.indexof ("?") = = 1?
		
				"?": "&"; url = url + paramprefix;
				For (var prop in data) {URL + prop + "=" + Data[prop] + "&";
			url = url.substring (0, url.length-1);
			} xmlhttp.open ("Get", url, async);
							if (async) {xmlhttp.onreadystatechange = function () {if (this.readystate==4) {if (this.status==200) {
							if (typeof params.success!= "undefined") {params.success (xmlhttp.responsetext); } else {if (typeof params.error!= "undefined") {Params.error (xmlhttp.status + xmlhttp.stat
							Ustext);
						} console.error (URL + ":" + xmlhttp.status);
			}
					}
				};
			} xmlhttp.send (NULL); if (!async) {if (xmlhttp.readystate = = 4 && xmlhttp.status =) if (typeof params.success!= "Undefin
					Ed ") {params.success (xmlhttp.responsetext);
					else {if (typeof params.error!= "undefined") {Params.error (xmlhttp.status + xmlhttp.statustext);
				} console.error (URL + ":" + xmlhttp.status);
						
}				
			}}, Createxmlhttprequest:function () {if window.
		XMLHttpRequest) {return new XMLHttpRequest (); else if (window.
		ActiveXObject) {//code for IE5 and IE6 return new 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 Date (). GetTime () + (parseint (Math.random () * 10000000000)); Eval ("Jsonpcallback_" + ff + "=" + function (data) {if (typeof params.success!= "undefined") {params.success (dat
			a);

		}
		}); Do you want to decide whether to add a timestamp parameter using "?" or "&" 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) {param + = prop + "=" + Data[prop] + "&";
		} param = 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";
		For Firefox, Google etc.
			Script.onerror = function () {if (typeof params.error!= "undefined") {params.error (); 
		} script.onload = function () {document.body.removeChild (script); }//For IE Script.onreadystatechange = function () {if (this.readystate = = "Loaded" | | this.readystate = = "com
		  Plete ") {document.body.removeChild (script);
 } 
		}
	}

};

The above is written in a JS Ajax library (example code) is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.