A simple Ajax class library and the use Method instance analysis _ajax correlation

Source: Internet
Author: User
Tags eval versions

This article illustrates a simple Ajax class library and how to use it. Share to everyone for your reference, specific as follows:

Ajax.js

function Ajax (recvtype) {var aj=new Object (); Aj.recvtype=recvtype?
  Recvtype.touppercase (): ' HTML '//html XML aj.targeturl= ';
  Aj.sendstring= ';
  Aj.resulthandle=null;
  Aj.createxmlhttprequest=function () {var request=false; A XMLHttpRequest presence in the Window object is not IE, including (IE7,IE8) if (window).
    XMLHttpRequest) {request=new XMLHttpRequest ();
    if (request.overridemimetype) {request.overridemimetype ("text/xml"); The ActiveXObject attribute exists in the//window object is IE}else if (window. ActiveXObject) {var versions=[' microsoft.xmlhttp ', ' MSXML. XMLHTTP ', ' msxml2.xmlhttp.7.0 ', ' msxml2.xmlhttp.6.0 ', ' msxml2.xmlhttp.5.0 ', ' msxml2.xmlhttp.4.0 ', ' MSXML2. xmlhttp.3.0 ', ' MSXML2.
    XMLHTTP '];
        for (var i=0 i<versions.length; i++) {try{request=new ActiveXObject (Versions[i]);
        if (request) {return request;
      }}catch (e) {request=false;
  }} return request; AJ.
  Xmlhttprequest=aj.createxmlhttprequest (); Aj.processhandle=function () {if (AJ. Xmlhttprequest.readystate = = 4) {if (AJ. Xmlhttprequest.status = = = {if (aj.recvtype== "HTML") Aj.resulthandle (AJ.
        Xmlhttprequest.responsetext); else if (aj.recvtype== "XML") Aj.resulthandle (AJ.
      Xmlhttprequest.responsexml);
    }} aj.get=function (TargetUrl, resulthandle) {aj.targeturl=targeturl; if (resulthandle!=null) {aj.
      Xmlhttprequest.onreadystatechange=aj.processhandle;
    Aj.resulthandle=resulthandle; } if (window. XMLHttpRequest) {aj.
      Xmlhttprequest.open ("Get", aj.targeturl); Aj.
    Xmlhttprequest.send (NULL); }else{AJ.
      Xmlhttprequest.open ("Get", Aj.targeturl, True); Aj.
    Xmlhttprequest.send ();
    } aj.post=function (TargetUrl, sendstring, resulthandle) {aj.targeturl=targeturl;
      if (typeof (sendstring) = = "Object") {var str= "";
      For (Var pro in sendstring) {str+=pro+ "=" +sendstring[pro]+ "&"; } aj.sendstring=str.substr (0, Str.lengtH-1);
    }else{aj.sendstring=sendstring; } if (Resulthandle!=null) {aj.
      Xmlhttprequest.onreadystatechange=aj.processhandle;
    Aj.resulthandle=resulthandle; AJ.
    Xmlhttprequest.open ("Post", TargetUrl); Aj.
    Xmlhttprequest.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Aj.
  Xmlhttprequest.send (aj.sendstring);
return to AJ;

 }

Use method:

<script type= "Text/javascript" > var ajax = Ajax (); Instantiate the object, default to HTTP, if incoming XML, return XML object//Ajax Get method Use description/** * Function (TargetUrl, resulthandle) * @param string TargetUrl passes past URL address * @param string Resulthandle callback function, Optional * * Ajax.get (' test.php?name=liruxing&email=liruxing1
    715@sina.com ', function (data) {eval ("var obj=" +data);
    alert (obj.name);
  alert (Obj.email);
  });  Ajax Post Method Use description/** * Function (TargetUrl, sendstring, resulthandle) * @param string TargetUrl pass past URL address * @param string sendstring parameter value * @param string Resulthandle callback function, Optional */ajax.post (' test.php ', ' Name=liruxing&emai
    L=liruxing1715@sina.com ', function (data) {eval ("var obj=" +data);
    alert (obj.name);
  alert (Obj.email);
    //Post the second format, the second parameter is Json format ajax.post (' test.php ', {name: ' Lijou star ', email: ' liruxing1715@sina.com '}, function (data) {
    Eval ("var obj=" +data);
    alert (obj.name);
  alert (Obj.email);

 }) </script>

For more information on AJAX-related content readers can view the site topics: "JQuery Ajax Usage Summary", "JavaScript Ajax Operating Skills summary", "Php+ajax Techniques and Application Summary" and "ASP.net Ajax skills Summary of the topic"

I hope this article will help you with Ajax programming.

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.