+ -
当前位置:首页 → 问答吧 → 超小型ajax实现

超小型ajax实现

时间:2010-08-10

来源:互联网

最基本的Ajax实现,初学者可以参考下,可以通过这个小函数扩展出复杂的功能类来
源代码下载地址:http://dev.mo.cn/article_285.html

JScript code
// Aien,[email protected],1034555083
// Ajax(a,b,c,d,e,f,g);
// 参数说明:a-请求方法;b-请求的地址;c-请求的数据,a为post时有效;d-回调函数,函数里面的this引用是xmlhttp对象;e-是否异步,除非设置为false,否则为异步调用;f,g-登录用户名和密码
function Ajax(a,b,c,d,e,f,g){
    a = a.toUpperCase();
    if(e===false){e=false;}else{e =
true;}
    var obj = Ajax.Ajax_GetObj();
    if(obj){
        if(f){
            obj.open(a,b,e,f,g);        
        }else{
            obj.open(a,b,e);        
        }        
        if(a.toUpperCase()=="POST"){
            obj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        
        }
        obj.onreadystatechange=function(){if(typeof d !=
"function"){return;}d.apply(obj,[a,b,c,d,e,f,g]);};
        obj.send(c);
    }               
}
Ajax.Ajax_GetObj =
function(){
    var b =
null;
    if (window.ActiveXObject) {  //for ie

var httplist = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
        for(var i = httplist.length -1;i >=
0;i--){
            try{
                b =
new ActiveXObject(httplist[ i]);
                return b;
            }catch(ex){}
        }
    }else
if (window.XMLHttpRequest) {  //for other
        b =
new XMLHttpRequest();
    }
    return b;               
}

作者: RICHARD   发布时间: 2010-08-10

Ajax(a,b,c,d,e,f,g)

作者: Fising   发布时间: 2010-08-10

相关阅读 更多

热门下载

更多