论坛首页 入门技术论坛

AJAX Starter Edition

浏览 1409 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-12   最后修改:2008-12-12

if(/msie/i.test(navigator.userAgent)) {
    window.XMLHttpRequest=function(){
        var msxmls=['MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','MICROSOFT.XMLHTTP.1.0','MICROSOFT.XMLHTTP.1','MICROSOFT.XMLHTTP'];
        for(var i=0;i<msxmls.length;i++){
            try{
                return new ActiveXObject(msxmls[i]);
            } catch(e){}
        }
        return null;
    };
}
Object.extend = function(destination, source) {
    for (var property in source)
        destination[property] = source[property];
    return destination;
};
var ajax = {
    send : function(arg){
        var req = new XMLHttpRequest();
        if(req==null){
            alert("The Browser don't support xmlhttp");
            return;    
        }
        if(req.overrideMimeTyp)req.overrideMimeType('text/xml');
        var dataType = {text:'responseText',xml:'responseXML',json:'responseText'};
        var options = {
            url : '',
            type : 'POST',
            async : true,
            data : null,
            timeout : 0,
            contentType : 'application/x-www-form-urlencoded',
            encoding : 'UTF-8',
            dataType : 'text',
            State : ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'],
            ErrorState :{"400": "Bad Request syntax error occurred",
                "404": "Can not find the location of the designated resources",
                "405": "type Not Allowed Request methods(GET、POST、HEAD、DELETE、PUT、TRACE and so on)Designation of resources don't apply"},
            create : function(){},
            success : function(){},
            doing : function(){},//From start to finish,-4 State
            timeout: function(){},
            error : function(status,ErrorMsg,eMsg){alert("Status: "+status+"\nErrors: "+ErrorMsg+"\n        "+eMsg);}
        }
        Object.extend(options,arg||{});
        options.url += ((options.url.indexOf("?")>0)?"&":"?")+"randnum=" + Math.random();
        options.type = options.type.toUpperCase();
        if(options.async){
            options.doing(0,options.State[0]);
            req.onreadystatechange=function(){
                var _state = req.readyState;
                options.doing(_state,options.State[_state]);
                switch(_state){
                    case 0:
                        options.create(_state,options.State[_state]);
                        break;
                    case 4:
                        if(req.status == 200)
                            options.success(options.dataType == "json" ? eval("("+req[dataType[options.dataType]]+")") : req[dataType[options.dataType]],_state,options.State[_state]);
                        else if( req.status >200 )
                            options.error(req.status,options.ErrorState[req.status],"");
                        req.onreadystatechange = function(){};
                        req = null;
                        break;
                }
            };
        }
        try{
            req.open(options.type, options.url, options.async);
            if(options.type=="POST")
                req.setRequestHeader('Content-Type', options.contentType+'; charset='+options.encoding);
            if(options.async && options.timeout>0){
                setTimeout(function(){
                    try{
                        if(req!=null && req.readyState!=4)  options.timeout();req.abort();req=null;
                    }
                    catch(e){}},options.timeout);
            }
            req.send(options.data);
            //Not as long as status=200,There can be called an error
            if(!options.async){
                options.success(req[dataType[options.dataType]]);
                req = null;
            }
        }
        catch(e){
            try{options.error(req.status,options.ErrorState[req.status],e);}
            catch(e){options.error(0,"",e);}
            finally{req.abort(); req=null;}
        }
    }
}

/////////////////////////////////////////////////////

        ajax.send({
            type: "POST", 
            url: "/aspx/common.aspx", 
            data: "action=keys&c=1&o="+o.id+"&nsid="+XmlPage.User()+"&q=" + escape(o.title) + "&s="+XmlPage.ParentId+"&p="+escape(o.p),
            dataType : "xml" ,
            async : true ,
            doing : function(req){
                if(takeinfo) takeinfo.innerHTML = XmlPage.Taking;
            },
            error: function(req){
                if(takeinfo) takeinfo.innerHTML = XmlPage.Takerror;
            },
            success: function(req){
                if(takeinfo) takeinfo.innerHTML = XmlPage.TakeOk;
                takesate.innerHTML = "<font color='red'>Subscribers have been</font>";
                XmlPage.DateSource = LoadXml("/js/subscribe.xml");
                XmlPage.init();
                setTimeout(function(){XmlPage.Show(XmlPage.CurrPage);},1000);
            }
        });

 

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics