The simple implementation of native AJAX encapsulation _ajax related

Source: Internet
Author: User
Tags ajax status code

Return to the original JS, the Internet to see the AJAX package, then take to change, I do not know what the drawbacks, I hope to point out!

var ajaxhelper = {/*1.0 browser-compatible way to create asynchronous object/Makexhr:function () {//Declare asynchronous object variable var xmlHttp = false; DECLARE extension var xmlhttpobj = ["msxml2.xmlhttp.5.0", "msxml2.xmlhttp.4.0", "msxml2.xmlhttp.3.0", "Msxml2.xmlhttp", "MSXML .
    XMLHttp "]; To determine whether the browser supports XMLHttpRequest, and if so, a new browser that creates the if window directly.
    XMLHttpRequest) {xmlHttp = new XMLHttpRequest (); //Otherwise, you can only iterate through the old browser asynchronous object name and try to create it until the creation is successful. else if window.
        ActiveXObject) {for (i = 0; i < xmlhttpobj.length; i++) {xmlHttp = new ActiveXObject (xmlhttpobj[i));
        if (xmlHttp) {break; Determine if the asynchronous object was created successfully and, if successful, return the asynchronous object or false return XmlHttp?
  Xmlhttp:false;
    /*2.0 Send Ajax Request * * Doajax:function (method, URL, data, Isayn, callback, type) {method = Method.tolowercase ();
    2.1 Create asynchronous object var xhr = THIS.MAKEXHR (); 2.2 Set the request parameter (if it is get, then with the URL parameter, if not, without) Xhr.open (method, url + (method = = "Got"?)
    "?" + Data: ""), Isayn); 2.3 According to pleasepredicate (get/post), add a different request header if (method = = "Get") {Xhr.setrequestheader ("if-modified-since", 0);
    else {xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
        //2.4 Set callback function Xhr.onreadystatechange = function () {//If the response message sent back by the server is received if (Xhr.readystate = 4) { Determine if the status code is normal if (xhr.status = =) {if (type.tolowercase () = "json") {var ret = {}
            ;
              try {if (typeof JSON!= "undefined") {ret = Json.parse (xhr.responsetext);
              } else {//IE8 does not support JSON ret = new Function ("return" + Xhr.responsetext) ();
            Callback (ret);
              catch (E) {console.log (e.message);
            Callback (FALSE);
          } else {//Direct return text callback (Xhr.responsetext); } else {Console.log ("AJAX Status Code:" + Xhr.staTUS);
        Callback (FALSE);
    }
      }
    };
  2.5 Send (if it is post, then pass the parameter, otherwise do not pass) Xhr.send (method!= "get"? data:null); /*3.0 send a POST request directly/dopost:function (URL, data, Isayn, callback, type) {This.doajax ("post", url, data, Isayn,
  callback, type); /*4.0 directly send GET request * * doget:function (URL, data, Isayn, callback, type) {This.doajax ("get", url, data, Isayn, Cal
  Lback, type); }
};

Assuming a requirement, the backend requires passing in two digits N1, N2, and then returns the sum.

When one of the arguments is empty or is not a number, returns: {"status": "0", "msg": "Wrong argument!" "}

When correct, return: {"status": "1", "sum": "//n1 plus n2"}

The back-end code is not posted.

Front-End calls:

document.getElementById ("Btnsubmit"). onclick = function () {
      ajaxhelper.dopost ("Backend url", "n1=10&n2=25", True, function (ret) {
        if (!ret) {return;}
        if (ret.status!= 1) {
          alert (ret.msg);
          return;
        }
        var n = ret.sum;
        var s = ret.status;
      }, "json");

The above native Ajax encapsulation of the simple implementation is a small series to share all the content, 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.