Ajax的访问组件 参照Spry重写的


var Think; if (!Think) Think = 
{} ;
if (!Think.Ajax) Think.Ajax = 
{} ;
Think.Ajax.XMLHTTP=function()
{
        if(HttpXml){HttpXml=null
}

        var HttpObj    =    ["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
        var HttpXml    =    null;
            try
{
                if(window.XMLHttpRequest){
                    HttpXml=new XMLHttpRequest();
                
}
else if(window.ActiveXObject) {
                        while(!HttpXml&&HttpObj.length){    
                        try{HttpXml=new ActiveXObject(HttpObj[0])
}
catch(Err) {HttpObj.shift();HttpXml=null} }
                }else
{Think.Ajax.deBug('XMLHTTP():没有找到相应的对象组件对象');HttpXml=null}
            }catch(ex)
{Think.Ajax.deBug('XMLHTTP():发生错误,错误描述',ex);HttpXml= null}
            return HttpXml;
        };
Think.Ajax.Load=function(cUrl,uData,successCallback,errorCallback,callback,options)
{
var rQ=new Think.Ajax.Load.Model();
    rQ.url                =cUrl;
    rQ.postData            =uData;
    if(successCallback){rQ.successCallback    =successCallback;
}

    if(errorCallback)
{rQ.errorCallback=errorCallback}
    if(callback)
{rQ.callback=callback}
    Think.Ajax.SetOptions(rQ,options);//获取rQ所有参数
    try
{
        rQ.xmObj=new Think.Ajax.XMLHTTP();
        if(!rQ.xmObj)return null;
        if(rQ.async==true){rQ.xmObj.onreadystatechange = function(){Think.Ajax.Load.Callback(rQ)
}
}
        rQ.xmObj.open(rQ.method, rQ.url, rQ.async,rQ.username,rQ.password);
        if(rQ.headers)
{for(var Name in rQ.headers){rQ.xmObj.setRequestHeader(Name,rQ.headers[Name])} }
        rQ.xmObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
        rQ.xmObj.send(rQ.postData);
        if(rQ.async==false)
{Think.Ajax.Load.Callback(rQ)}
    }catch(ex)
{
        Think.Ajax.deBug('XMLHTTP()
:发生错误,错误描述',ex)
    
}

};
Think.Ajax.Load.Model=function()
{
    var Pars    =Think.Ajax.Load.Parameters;
    var ParsLen    =Pars.length;
    for(var i=0;i<ParsLen;i++){this[Pars[i]]=null
}

    this.method    ="post";
    this.async    =true;
    this.headers= 
{} ;
};
Think.Ajax.Load.Parameters    =[ "method", "url", "async", "username", "password", "postData", "successCallback", "errorCallback","callback", "headers", "userData", "xmObj" ];
Think.Ajax.Load.readystate    =['未初始化','初始化','发送请求','等待数据','请求完成'];
Think.Ajax.Load.Status=
{
    200
:"数据加载成功",
    404:"服务器未找到指定文件",
    401:"请求授权失败",
    500:"服务器发生异常状况",
    505:"当前浏览器不支持",
    12007:"服务器出错"
}
;
Think.Ajax.Load.Callback=function(rQ)
{
    if(rQ&&rQ.callback){rQ.callback(rQ)
}
else {Think.Ajax.Status(rQ)}
    if(!rQ||rQ.xmObj.readyState!=4) return;
    if(rQ.successCallback&&(rQ.xmObj.status == 200 || rQ.xmObj.status == 0))
{rQ.successCallback(rQ.xmObj);}
    else if(rQ.errorCallback)
{
        rQ.errorCallback(rQ);
    
}
else if(rQ.xmObj.status != 200) {
        Think.Ajax.Error(rQ);
    
}
};
Think.Ajax.SetOptions=function(Obj,optionObj)
{
    if(!Obj||!optionObj)return;
    for(var Name in optionObj){
        if(!optionObj[Name]){continue
}

        Obj[Name]=optionObj[Name];
    }};
Think.Ajax.Error=function(rQ)
{
    var msg=Think.Ajax.Load.Status[rQ.xmObj.status];
    if((typeof msg!="undefined")&&msg!=""){
        msg=rQ.xmObj.status+":"+Think.Ajax.Load.Status[rQ.xmObj.status];
    
}
else {
        msg=rQ.xmObj.status+":"+rQ.xmObj.statusText;
    
}

    Think.Eds.MsgBox(msg);
};
Think.Ajax.Status=function(rQ)
{
    Think.Eds.MsgBox(Think.Ajax.Load.readystate[rQ.xmObj.readyState]);
    window.setTimeout("Think.Eds.MsgBox.Clearn()",10000);
}
;
Think.Ajax.deBug=function(msg,ex)
{
    Think.Eds.MsgBox(msg+"<br>"+ex.description);
}
;
if(!Think.Eds) Think.Eds   =    
{} ;
       Think.Eds.MsgBox    =    function(Msg,Mid)
{
        if(document.getElementById('Eds_gState')!=null){document.getElementById('Eds_gState').innerHTML=Msg;return
}

            var BorderTop="0px solid #009900";
            var gStateObj=document.createElement("div");
                gStateObj.setAttribute("id","Eds_gState");
                gStateObj.setAttribute("align","center");//管理内部文本
                gStateObj.style.position="absolute";//定位方式
                //gStateObj.style.float="right";
                gStateObj.style.top="10px";
                gStateObj.style.right="10px";
                gStateObj.style.background="#ff3300";
                gStateObj.style.borderTop=BorderTop;
                gStateObj.style.color="#FFFFFF";
                gStateObj.style.padding="4px,0px,0px,0px";
                gStateObj.style.width="120px";
                gStateObj.style.height="";
                gStateObj.style.zIndex="12006";
                gStateObj.style.fontSize="12px";
                gStateObj.style.fontFamily = "宋体";
                gStateObj.style.overflow="auto";
                gStateObj.innerHTML=Msg;
                document.body.appendChild(gStateObj)
        }
    Think.Eds.MsgBox.Clearn    =    function()
{
        if(document.getElementById('Eds_gState')!=null){document.body.removeChild(document.getElementById('Eds_gState'))
}

    }
if(!Think.Xml)Think.Xml=
{} ;
    Think.Xml.XmlDoc=function(Path,Callback)
{//加载一个XML文档
        if(XmlDoc)XmlDoc=null;
        var XmlObj        =    ["MSXML2.DOMDocument.5.0","MSXML2.DOMDocument.4.0","MSXML2.DOMDocument.3.0","MSXML2.DOMDocument","Microsoft.XMLDOM"];
        var XmlDoc        =    null;
            while(!XmlDoc&&XmlObj.length){
                try{var XmlDoc    =    new ActiveXObject(XmlObj[0])
}
catch(Err) {XmlObj.shift();XmlDoc=null}
                }
                XmlDoc.async=true;
                XmlDoc.onreadystatechange=function()
{} ;
                XmlDoc.load(Path);
                return XmlDoc;
        }//End XmlDoc

你可能感兴趣的:(Ajax)