ajax兼容ie低版本

//制作一个方法
function myajax(method,ajHtml,ajText,fn) {
    //创建一个新的对象用于存储ajax
    let xhr=null;
    //判断浏览器中有没有XMLHttpRequest对象,如果没有,就调用ie的对象赋值
    if (window.XMLHttpRequest){
        xhr=new XMLHttpRequest();
    }else {
        xhr=new ActiveXObject("Msxm12.XMLHTTP");
    }
    //判断是get还是post传递方式,并调用对应的方法
    if (method=="get"){
        xhr.open("get",ajHtml,true);
        xhr.send()
    } else if (method=="post") {
        xhr.open("post",ajHtml,true);
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
        let date=ajText||"";
        xhr.send(date);
    }
    xhr.onreadystatechange=function () {
        if (xhr.readyState==4){
            if (xhr.status==200){
                //设置一个方法给外部参数调用
                fn(xhr.responseText);
            }
        }
    };
}

猜你喜欢

转载自blog.csdn.net/chenzhuozhu/article/details/81022788