一個封裝的Ajax類

來源:互聯網
上載者:User
ajax|封裝

用法: 
new Ajax().Request(url,cmd,async,method,postString,title) 
參數: 
url: 請求頁面URL(必填) 
cmd: 傳回值處理函數(必填) 
async: 是否非同步 ,(ture|false), 預設true 
method: 請求方式,(post|get), 預設get 
postString: 請求方式為post時,請求內容 
title: 請求內容標題 


[複製此代碼]CODE:
// Ajax 封裝 2007-3-13
function createXMLHttpRequest() {
    try {        
            if (window.XMLHTTPRequest) {
                    return new XMLHttpRequest();
            }
            else if (window.ActiveXObject) {
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        catch (e) {alert("XMLHttpRequest對象無法建立!請檢查IE安全設定!");}
}
function messageDiv(t)
{
    var v = document.createElement("<div>");
    v.innerHTML = "<table style=\"width:300px;\" id=message>" +
                  "<tr style=\"font-size:12px;background-color:#EEEEff;color:#227933;height:20px\">" + 
                  "<td style=\"padding:2px;border-top:1px solid #E1E1E1;border-left:1px solid #E1E1E1;border-bottom:1px solid #818181;border-right:1px solid #A1A1A1\">" +
                  "<nobr><img src=refresh.gif align=absmiddle> " + t + ",<span id=Span1>串連未初始化...</span></nobr></td></tr></table>";
    var l = document.getElementsByName("message").length;
    v.style.cssText = "position:absolute;bottom:" + (l*24) + "px;left:0px;display:none";
    document.body.appendChild(v);
    this.clear = function () {
        document.body.removeChild(v);
        var msg = document.getElementsByName("message");
        for (var i=0;i<msg.length;i++){
            msg[i].parentNode.style.cssText = "position:absolute;bottom:" + (i*24) + "px;left:0px";
        }
    }
    this.showmsg = function (s) {
        v.style.display = "";
        v.all.Span1.innerHTML = s;
    }
}
function Ajax() {
    var x = new createXMLHttpRequest();
    this.Request = function (url,cmd,async,method,postString,title) {
        if (method!="post") method = "post"; else method = "get";
        if (async!=true) async = true; else async = false;
        if (typeof(postString)!="string") postString="";
        if (typeof(title)!="string") title="正在擷取資料"; else title="正在擷取" + title;
        var msgbox = new messageDiv(title);
        x.onreadystatechange = function ()
        {
            if (async) switch (x.readystate) {
            case 1:
                msgbox.showmsg("正在初始化串連...");
                return;
            case 2:
                msgbox.showmsg("正在發送資料...");
                return;
            case 3:
                msgbox.showmsg("正在接收資料...");
                return;
            case 4:
                msgbox.showmsg("資料接收完成...");
                if (x.status == 200) {
                    cmd(x.responseText);
                    msgbox.clear();
                }
                else {
                    msgbox.showmsg("請求失敗," + x.statustext + "(" + x.status + ")");
                    setTimeout(msgbox.clear,3000);
                }
                return;
            }
        }
        x.open (method,url,async);
        if (method=="post") {msgbox.showmsg("正在接收資料...");x.send(postString);} else x.send();
        if (!async) {
            msgbox.showmsg("資料接收完成...");
            cmd(x.responseText);
            msgbox.clear();
        }
    }    
}

 



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.