当前位置 : 主页 > 网络编程 > JavaScript >

[JavaScript][AJAX]封装好的AJAX类

来源:互联网 收集:自由互联 发布时间:2021-06-30
(OOP封装)AJAXScript.js /*通用发送模型*/function MyAJAX(){ var ClassType=['Abstruct']; this.msg = undefined; this.xmlHttpInstance = undefined;}MyAJAX.prototype.build = function () { //创建一个xmlHttpRequest var xmlHttp; if (windo
(OOP封装)AJAXScript.js
/*通用发送模型*/
function MyAJAX(){
    var ClassType=['Abstruct'];
    this.msg = undefined;
    this.xmlHttpInstance = undefined;
}
MyAJAX.prototype.build = function () {
    //创建一个xmlHttpRequest
    var xmlHttp;
    if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest();
    else xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    return xmlHttp;
};
MyAJAX.prototype.sendRequest = function (how2Send,xmlHttp,url) {
    //根据既定方法发送请求
    xmlHttp.open(how2Send,url,true);
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
};
MyAJAX.prototype.post = function (url,sendMsg) {
    //用post方法发送请求
    var xmlHttp = this.build();
    var instanceType = this;

    this.sendRequest("POST",xmlHttp,url);
    xmlHttp.send(sendMsg);

    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState===4 && xmlHttp.status===200){
            instanceType.accessedCallback(xmlHttp);
        }else{
            instanceType.failedCallback(xmlHttp);
        }
    }
};
MyAJAX.prototype.get = function (url) {
    //用get方法发送请求
    var xmlHttp = this.build();
    var instanceType = this;

    this.sendRequest("GET",xmlHttp,url);
    xmlHttp.send();

    xmlHttp.onreadystatechange = function () {
        instanceType.xmlHttpInstance = xmlHttp;
        if (xmlHttp.readyState===4 && xmlHttp.status===200){
            instanceType.accessedCallback(xmlHttp);
        }else{
            instanceType.failedCallback(xmlHttp);
        }
    }
};
MyAJAX.prototype.accessedCallback = function (xmlHttp) {
    this.dispatchMsg(xmlHttp);//成功回应后的分发接口
};
MyAJAX.prototype.failedCallback = function (xmlHttp) {
    //alert("xml接收失败!");//可重写
};
MyAJAX.prototype.dispatchMsg = function (xmlHttp) {
    var FunctionType=['Abstruct'];
    //由子类实现
};
MyAJAX.prototype.isReady = function () {
    //当有数据发送来之后
    return this.msg !== undefined;
};
MyAJAX.prototype.getData = function () {
    //用户获取数据的方法
    return this.msg;
};
MyAJAX.prototype.getXmlHttp= function () {
    return this.xmlHttpInstance;
};

//-----两个常用的具体实现类-----//
/*XML的AJAX请求消息(XMLDoc)*/
function XmlAJAX() {
    MyAJAX.call(this);
}
XmlAJAX.prototype = Object.create(MyAJAX.prototype);
XmlAJAX.prototype.constructor = XmlAJAX;
XmlAJAX.prototype.dispatchMsg = function (xmlHttp) {
    var FunctionType=['Override'];
    this.msg = xmlHttp.responseXML;
};

/*普通AJAX请求消息(TEXT)*/
function MsgAJAX() {
    MyAJAX.call(this);
}
MsgAJAX.prototype = Object.create(MyAJAX.prototype);
MsgAJAX.prototype.constructor = MsgAJAX;
MsgAJAX.prototype.dispatchMsg = function (xmlHttp) {
    var FunctionType=['Override'];
    this.msg = xmlHttp.responseText;
};
回调接口式的封装.js
//-------回调接口的封装--------//
//详见 http://blog.csdn.net/shenpibaipao/article/details/78160994
function AJAXpost(url,sendMsg,callback) {
    var xmlHttp;
    if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest();
    else xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send(sendMsg);
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState===4 && xmlHttp.status===200){
            callback(xmlHttp);
        }else{
            //失败的回调接口,可以自己定义。一般情况下用不到。
        }
    }
}
function AJAXget(url,callback) {
    var xmlHttp;
    if (window.XMLHttpRequest) xmlHttp=new XMLHttpRequest();
    else xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    xmlHttp.open("GET",url,true);
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send();
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState===4 && xmlHttp.status===200){
            callback(xmlHttp);
        }else{
            //失败的回调接口,可以自己定义。一般情况下用不到。
        }
    }
}
网友评论