Encapsulate instance _ajax related to Ajax network requests

Source: Internet
Author: User
Tags script tag

Instance code:

Encapsulated AJAX Network Request function//obj is an object function AJAX (obj) {//Cross-domain request if (Obj.datatype = "Jsonp") {//here callback must be It's a global variable. The variable cannot be destroyed//processed when the function name (prevents multiple network request functions from having the same name disorder) var hehe = "CallBack" + "_" + New Date (). Gettim
      E () + "_" + String (Math.random ()). Replace (".", "");
      Window[hehe] = obj.success;
      Create a script tag var sc = document.createelement ("script");
      SC.SRC = Obj.url + "?" + "cb=" + hehe;
      Console.log (SC.SRC);
      Document.body.appendChild (SC);
      Document.body.removeChild (SC);
    Return
  {///1, create Ajax object var ajaxobj = null; if (window.
  XMLHttpRequest) {ajaxobj = new XMLHttpRequest ();
  }else{ajaxobj = new ActiveXObject ("Microsoft.XMLHTTP"); //Set the type of request Obj.type = Obj.type.toUpperCase () | |

  "Get"; If it is a GET request and you need to pass the parameter, you need to give the URL a concatenation parameter if (Obj.type = = "Get") {var arr = [];//definition array is used to store the object in the data for the (Var key in OB
    J.data) {Arr.push (key + "=" + Obj.data[key]); ///With & spacer array Let it be transformed into something like this: the name=lxl&age=18 form var str = arr.join ("&"); Obj.url = Obj.url + "?"
    + str;


    Dial-up Ajaxobj.open (obj.type,obj.url,true);
  Send "Name=123&age=18" ajaxobj.send ();
      }else{var arr = [];//definition array is used to store objects into the data for (Var key in Obj.data) {Arr.push (key + "=" + Obj.data[key]);
    Console.log (arr);
    ///Use & spacer array to convert it to resemble: name=lxl&age=18 form var str = arr.join ("&");
    Console.log (str);
    Ajaxobj.open (obj.type,obj.url,true);
    Ajaxobj.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
  Ajaxobj.send (str); }//Listener Ajaxobj.onreadystatechange = function () {if (ajaxobj.readystate = 4) {if (Ajaxobj.status >= && Ajaxobj.status < 300 | |
      Ajaxobj.status = = 304) {//Request successful obj.success (Ajaxobj.responsetext);
      }else{//Request Failure Obj.error (ajaxobj.status);
 }
    }

  }

}

Above the AJAX network request for the package example 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.