js封装ajax

function ajax(method,url,boolean,data,fn) {
    // 兼容
    var xhr=null;
    if(window.XMLHttpRequest){
        xhr=new XMLHttpRequest();
    }
    else{
        xhr=new ActiveXObject("Microsoft.XMLHTTP")
    }
    if(method=="get") {
        var url = url + "?" + data;
        xhr.open(method, url, true);
        xhr.send();
    }
    else  if(method=="post"){
        xhr.open(method, url, true);
        xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
        xhr.send(data);
    }
    xhr.onreadystatechange=function () {
        if(xhr.readyState==4&&xhr.status==200){
            fn(xhr.responseText);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42413684/article/details/80975653
今日推荐