栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript > Ajax教程

一个封装的Ajax类

Ajax教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一个封装的Ajax类

用法: 
new Ajax().Request(url,cmd,async,method,postString,title) 
参数: 
url: 请求页面URL(必填) 
cmd: 返回值处理函数(必填) 
async: 是否异步 ,(ture|false), 默认true 
method: 请求方式,(post|get), 默认get 
postString: 请求方式为post时,请求内容 
title: 请求内容标题 
复制代码 代码如下:
// 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("");
    v.innerHTML = "" +
                  "" + 
                  "" +
                  " " + t + ",连接未初始化...";
    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[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();
        }
    }    
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/129054.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号