axja异步请求的方法

2018-06-08  本文已影响0人  陈哲_b5da

//异步请求的方法
方法一:

var AjaxExecute = function (url, formData, callFun, token, isasync) {
    if (isasync == undefined) isasync = true;
    var data = JSON.stringify(formData);
    $.ajax({
        url: url, // url action是方法的名称
        cache: false,
        data: data,
        async: isasync,
        type: 'POST',
        dataType: 'JSON',
        contentType: "application/json; charset=utf-8",
        beforeSend: function (request) {
            if (token != undefined) {
                request.setRequestHeader("Authorization", "Bearer " + token);
            }
        },
        success: function (data) {
            if (data != null) {
                if ($.isFunction(callFun)) {
                    if (data.Code == "200") {
                        if (data.Result != null) {
                            callFun(data.Result);
                        } else {
                            tips(data.Msg);
                        }
                    } else {
                        tips(data.Msg);
                    }
                }
            } else {
            }
        },
        error: function (msg) {
            tips(msg);
        }
    });
};

、、方法二:

var FnAjaxExecute = function (url, formData, callFun, token, isasync) {
    if (isasync == undefined) isasync = true;
    var data = JSON.stringify(formData);
    $.ajax({
        url: url, // url action是方法的名称
        cache: false,
        data: data,
        async: isasync,
        type: 'POST',
        dataType: 'JSON',
        contentType: "application/json; charset=utf-8",
        beforeSend: function (request) {
            if (token != undefined) {
                request.setRequestHeader("Authorization", "Bearer " + token);
            }
        },
        success: function (data) {
            callFun(data);
        },
        error: function (msg) {
            tips(msg);
        }
    });
};
    //获取基础数据
  AjaxExecute(Url, {key1:value2;key2:value2 }, function (data) {
        //console.log(data);
})
上一篇 下一篇

猜你喜欢

热点阅读