妙味——ajax(2)

封装的ajac库

function ajax(url,fnSucc,fnFaild){
    // 1、创建Ajax对象    XMLHttpRequest在IE6下用不了

    var oAjax=null;
        if(window.ActiveXObject){
            oAjax = new ActiveXObject('Microsoft.XMLHTTP');
        }
        else{
            oAjax = new XMLHttpRequest();
        }


    //2、链接服务器
    // open(方法,url,是否是异步)
    oAjax.open('GET', url, true);

    //3、发送请求
    oAjax.send();

    // 4、接收返回
    oAjax.onreadystatechange=function(){
        // alert(oAjax.onreadyState);
        if(oAjax.readyState==4){
            // alert(oAjax.status);
            if(oAjax.status=200){
                // alert('成功'+oAjax.responseText);
                fnSucc(oAjax.responseText);
            }
            else{
                // alert('失败');
                if(fnFail){
                    fnFaild();    
                }
            }
        }
    };
}
高否?富否?帅否? 否? 滚去学习!
原文地址:https://www.cnblogs.com/baixc/p/3475926.html