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

     

    自己封装的Ajax - JavaScript


    演示:http://www.huiyi8.com/jiaoben/

    源于:http://www.huiyi8.com/sc/16885.html

    //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{
                    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();
        };
    }

  • 相关阅读:
    [转]权限管理模型
    如何解决 “invalid resource directory name”, resource “crunch”
    Oracle ERP Interface堵住--Request Running too long time,查找Request执行的Sql
    【转】令人印象深刻的廣告詞
    【异常】SQL Server blocked access to STATEMENT OpenRowset/OpenDatasource
    你是喜欢一个人本身,还是
    关于Oracle 的安装记(不定期更新)
    kafka
    mysql去除换行和空格
    mysql 字段值拼接,同一字段循环拼接
  • 原文地址:https://www.cnblogs.com/lhrs/p/4155263.html
Copyright © 2011-2022 走看看