ajax 的js引用

function ajax(method,url,data,success){

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

    if(method=="get"&&data){
        url+="?"+data;
    }
    //第二步:打开文件
    oAjax.open(method,url,true);
    //第三步:发送
    //如果是get方式
    if(method=="get"){
        oAjax.send()
    }else{ //如果是post,需要设置头部,声明文档类型
        oAjax.setRequestHeader('content-type','Content-Type", "application/x-www-form-urlencoded')
        oAjax.send(data)
    }
    //第四步:返回内容
    oAjax.onreadystatechange=function(){
        if(oAjax.readyState==4){
            if(oAjax.status==200){
                //如果成功且执行的函数
                success(oAjax.responseText)
            }else{
                alert('出错,ERR:'+oAjax.status)
            }
        }
    }
}
原文地址:https://www.cnblogs.com/mylove0/p/7482284.html