xmlhttp對象的簡單封裝

來源:互聯網
上載者:User

xmlhttp對象好用,但是一個頁面如果有很多次調用的話,寫起來也很麻煩,所以,這裡簡單封裝了一下,可以作為對象來調用
ajax.js:
function ajaxsz() {
    var xmlHttp;
    if(window.XMLHttpRequest) {   
       xmlHttp = new XMLHttpRequest();
   }
   else if(window.ActiveXObject) {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
    }
    this.rep = xmlHttp;
    this.sendData = function(method,URL,asy,fun) {
        if (asy)//非同步
        {
            xmlHttp.onreadystatechange = function(){//此處檢測狀態,知道符合要求時,才會執行事件             
             if(xmlHttp.readyState==4 && xmlHttp.status == 200){
        fun(xmlHttp);
             }            
           }
            if(method == "POST") {
                xmlHttp.open("POST",URL,true);
                xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                xmlHttp.send(null);
            }
            else {
                 xmlHttp.open("GET",url,true);
                 xmlHttp.send(null);
            }
        }   
        else {
            if(method == "POST") {
                xmlHttp.open("POST",URL,false);
                xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                xmlHttp.send(null);
            }
            else {
                xmlHttp.open("GET",url,false);
                xmlHttp.send(null);
            }
            return xmlHttp.responseText;
        }  
    }
}

伺服器端簡單處理:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["UserName"]))
            {
                Response.Write("您好!" + Request["UserName"]);
            }
            else
            {
                Response.Write("您好!");
            }
        }

調用頁面:
<html>
<head>
    <title>Ajax測試頁</title>
    <script type="text/javascript" src="js/ajax.js" language="javascript"></script>
    <script language="javascript" type="text/javascript">
    function getStr(){
        var userName = document.getElementById("Text1").value;
        var myajax = new ajaxsz();
        myajax.sendData("POST","http://localhost:5964/GetData/GetString.aspx?UserName=" + userName,true,aa);
    }    
    function aa(xmlhttp)
    {
        document.getElementById("mystr").innerHTML = xmlhttp.responseText;
    }
    </script>
</head>
<body style="font-size: 14px" text="#6600ff">
        <div style="margin:0px auto">
            返回字串<br />
            &nbsp;<br />
            <div id="mystr" style="width: 121px; height: 34px">
            </div>
            輸入您的姓名<input id="Text1" style="width: 125px" type="text" />
            <input id="Button1" type="button" value="GetString" onclick="getStr()" />
        </div>
</body>
</html> 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.