Use native js to write ajax

Source: Internet
Author: User

// Use native js to encapsulate ajax // compatible with xhr object function createXHR () {if (typeof XMLHttpRequest! = "Undefined") {// non-IE6 browser return new XMLHttpRequest ();} else if (typeof ActiveXObject! = "Undefined") {// IE6 browser var version = ["MSXML2.XMLHttp. 6.0 "," MSXML2.XMLHttp. 3.0 "," MSXML2.XMLHttp ",]; for (var I = 0; I <version. length; I ++) {try {return new ActiveXObject (version [I]);} catch (e) {// skip} else {throw new Error ("your system or browser does not support XHR objects! ") ;}// Escape Character function params (data) {var arr = []; for (var I in data) {arr. push (encodeURIComponent (I) + "=" + encodeURIComponent (data [I]);} return arr. join ("&") ;}// encapsulate ajaxfunction ga_ajax (obj) {var xhr = createXHR (); obj. url = obj. url + "? Rand = "+ Math. random (); // clear the cached obj. data = params (obj. data); // escape string if (obj. method = "get") {// determines whether the get method is used to send obj. url + = obj. url. indexOf ("? ") ="-1 "? "? "+ Obj. data: "&" + obj. data;} // asynchronous if (obj. async = true) {// The onreadystatechange event xhr must be triggered during asynchronous processing. onreadystatechange = function () {// execution completed if (xhr. readyState = 4) {callBack () ;}} xhr. open (obj. method, obj. url, obj. async); // false is synchronous; true is asynchronous // "demo. php? Rand = "+ Math. random () + "& name = ga & ga", if (obj. method = "post") {xhr. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); xhr. send (obj. data);} else {xhr. send (null);} // xhr. abort (); // cancel asynchronous request // synchronize if (obj. async === false) {callBack () ;}// return data function callBack () {// determine whether to return correct if (xhr. status = 200) {obj. success (xhr. responseText);} else {obj. error ("failed to get data, Error code:" + xhr. status + "error message:" + xhr. statusText) ;}}var html = document. getElementsByTagName ("html") [0]; html. onclick = function () {ga_ajax ({"method": "post", "url": "dem0o. php "," data ": {" name ":" gaga "," age ": 10000000," num ":" 12346 & 598 "}," success ": function (data) {alert (data) ;}, "Error": function (text) {alert (text) ;}, "async": false });}

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.