zoukankan      html  css  js  c++  java
  • 自己封装的Ajax

    1. [代码][JavaScript]代码     
     //javascript Object: ajax Object
    //Created By RexLee
    function Ajax(url,data){
        this.url=url;
        this.data=data;
        this.browser=(function(){  
            if(navigator.userAgent.indexOf("MSIE")>0) {  
                return "MSIE";//IE浏览器
            }else{
                return "other";//其他
        }})();
    };
    Ajax.prototype={
        get:function(){
            var result;
            var xmlhttp;
            if(this.browser=='MSIE'){
                try{
                    xmlhttp=new ActiveXObject('microsoft.xmlhttp');
                    }catch(e){
                        xmlhttp=new ActiveXObject('msxml2.xmlhttp');
                        }
                }else{
                    xmlhttp=new XMLHttpRequest();
                };
            xmlhttp.onreadystatechange=function(){
                result = xmlhttp.responseText;//闭包,不能采用this.属性
            };
            xmlhttp.open('GET',this.url+'?'+this.data,false);//true无法抓取数据,why?
            xmlhttp.send(null);
            return result;
        },
        post:function(){
            var result;
            var xmlhttp;
            if(this.browser=='MSIE'){
                xmlhttp=new ActiveXObject('microsoft.xmlhttp');
                }else{http://www.huiyi8.com/webyuanma/
                    xmlhttp=new XMLHttpRequest();
                };网站源码下载
            xmlhttp.onreadystatechange=function(){
                result = xmlhttp.responseText;//闭包,不能采用this.属性
            };
            xmlhttp.open('POST',this.url,false);//需设为false,否则无法抓取responseText
            xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//POST中,这句必须
            xmlhttp.send(this.data);
            return result;
        }
    };
     
    //var a=new ajax('opp2.js','');
    //alert('by GET\n'+a.get())
    //alert('by POST\n'+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();
        };
    }

  • 相关阅读:
    概率与数学期望初步
    $Luogu$ $P4316$ 绿豆蛙的归宿(附期望 $dp$ 的设计总结)
    $Luogu$ $P4427$ $[BJOI2018]$ 求和
    $SP3978$ $DISQUERY$ $-$ $Distance$ $Query$
    最近公共祖先模板(未完待续)
    $Luogu$ $P3052$ $[USACO12MAR]$ 摩天大楼里的奶牛 $Cows$ $in$ $a$ $Skyscraper$
    $Luogu$ $P2622$ 关灯问题 $mathrm{II}$
    [转载] $CF633F$ 题解
    [转载] $Luogu$ $P3933$ 题解
    2020高考回忆录(随便写写
  • 原文地址:https://www.cnblogs.com/xkzy/p/3822560.html
Copyright © 2011-2022 走看看