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);
            }
        }
    };
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章