AJAX工具

代码如下

var AppAjax ={
    baseUrl:AppConfig.apiUrl
    //【POST请求】
    ,post:function(pUrl,pData,pSuccessFun){
        pUrl = AppAjax.baseUrl + pUrl;
        $.ajax({
            headers: {
               token: AppConfig.token
            },
            url:pUrl,
            type:'POST',
            data:JSON.stringify(pData),//pData,//JSON.stringify(),
            contentType:"application/json",
            beforeSend:function (request) {
            },
            success:function(respondData){
                if(AppAjax.responseFormat(pUrl,respondData)){
                    pSuccessFun(respondData);
                }
            },
            error:function(respondData){
                AppAjax.responseFormat(pUrl,respondData)
            }
        });

    }
    //【GET请求】
    ,get:function(pUrl,pSuccessFun){
        pUrl = AppAjax.baseUrl + pUrl;
        $.ajax({
            headers: {
                token: AppConfig.token
            },
            url:pUrl,
            type:'GET',
            contentType:"application/json",
            beforeSend:function (request) {
            },
            success:function(respondData){
                if(AppAjax.responseFormat(pUrl,respondData)){
                    pSuccessFun(respondData);
                }
            },
            error:function(respondData){
                AppAjax.responseFormat(pUrl,respondData)
            }
        });
    }
    //【请求统一处理】
    ,responseFormat:function(url,respondData){
        //console.log(respondData)
        if(respondData.status == 404){
            alert("接口404")
        }else if( respondData.success === false){
            alert(respondData.msg)
            if(respondData.code == 401){
                window.location.href = AppConfig.loginPage;
                alert("重新登录")
            }else{
                $("body").html("<div align='center'><h1>"+respondData.msg+"</h1></div>")
            }

        }else{

        }

        return true;

    }
}

 

posted @ 2019-06-18 13:50  Devan.Yan  阅读(311)  评论(0编辑  收藏  举报