Implement AJAX encapsulation Sample sharing yourself

Source: Internet
Author: User

  This article mainly introduces its own implementation of AJAX encapsulation examples, need friends can refer to the following

The code is as follows:  //javascript object:ajax Object//created by Rexlee function Ajax (url,data) {    this.url=url; &nb Sp   This.data=data;     this.browser= (function () {         -if (Navigator.userAgent.indexOf ("MSIE") >0 {              return "MSIE";//ie browser      }else{  &NBSP ;         return "others";/Other    }} (); }; ajax.prototype={    Get:function () {        var result;         VAR xml http         if (this.browser== ' Msie ') {            try{                xmlhttp=new activexobject (' microsoft.xmlhttp ');                }catch (e) {                    xmlhttp=new ActiveXObject (' msxml2.xmlhttp ');       &nbsp                        }else{        & nbsp       xmlhttp=new XMLHttpRequest ();            };         xmlhttp.onreadystatechange=function () {            result = XMLH ttp.responsetext;//closures, cannot use this. Attribute        };         Xmlhttp.open (' Get ', this.url+ '? ') +this.data,false);//true cannot crawl data, why?         xmlhttp.send (null);         return result;    },     post:function () {        var result;         var xm Lhttp;         if (this.browser== ' Msie ') {            xmlhttp=new ActiveXObject ( ' Microsoft.XMLHTTP ');            }else{                Xmlhttp=new XMLHT TpRequest ();            };         xmlhttp.onreadystatechange=function () {            result = XMLH ttp.responsetext;//closures, cannot use this. Attribute        };         Xmlhttp.open (' POST ', this.url,false);//Must be set to false, otherwise unable to crawl responsetext         Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");//post, this must be         Xmlhttp.send (this.data);         return result;    }};  //var a=new Ajax (' Opp2.js ', '); Alert (' by Getn ' +a.get ())//alert (' by Postn ' +a.post ());       Window.onload=function () {document.getElementById ("btn"). Onclick=function () {    var P=document.getelementbyid ("T"). Value     var a=new Ajax ("phpoop/ Getpage.php "," page= "+p);     document.getElementById ("box"). Innerhtml=a.get ();    }; }
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.