主頁 > 知識庫 > 一個封裝的Ajax類

一個封裝的Ajax類

熱門標(biāo)簽:服務(wù)器配置 團(tuán)購網(wǎng)站 電子圍欄 科大訊飛語音識別系統(tǒng) Linux服務(wù)器 Mysql連接數(shù)設(shè)置 阿里云 銀行業(yè)務(wù)

用法: 
new Ajax().Request(url,cmd,async,method,postString,title) 
參數(shù): 
url: 請求頁面URL(必填) 
cmd: 返回值處理函數(shù)(必填) 
async: 是否異步 ,(ture|false), 默認(rèn)true 
method: 請求方式,(post|get), 默認(rèn)get 
postString: 請求方式為post時,請求內(nèi)容 
title: 請求內(nèi)容標(biāo)題 

復(fù)制代碼 代碼如下:

// 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對象無法創(chuàng)建!請檢查IE安全設(shè)置!");}
}
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;imsg.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="正在獲取數(shù)據(jù)"; 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("正在發(fā)送數(shù)據(jù)...");
                return;
            case 3:
                msgbox.showmsg("正在接收數(shù)據(jù)...");
                return;
            case 4:
                msgbox.showmsg("數(shù)據(jù)接收完成...");
                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("正在接收數(shù)據(jù)...");x.send(postString);} else x.send();
        if (!async) {
            msgbox.showmsg("數(shù)據(jù)接收完成...");
            cmd(x.responseText);
            msgbox.clear();
        }
    }    
}

您可能感興趣的文章:
  • ajax的工作原理以及異步請求的封裝介紹
  • js原生Ajax的封裝和原理詳解
  • 基于jQuery的ajax方法封裝
  • 原生Javascript封裝的一個AJAX函數(shù)分享
  • 封裝了jQuery的Ajax請求全局配置
  • 自己動手封裝的 ajax
  • 關(guān)于Ajax的原理以及代碼封裝詳解

標(biāo)簽:棗莊 江蘇 大理 蚌埠 衢州 廣元 萍鄉(xiāng) 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《一個封裝的Ajax類》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266