美文网首页
AJAX封装

AJAX封装

作者: 丸子_d7e2 | 来源:发表于2019-07-15 17:10 被阅读0次
#var agent = function(options) {
  var Ajax = function (options) {
    var timestamp = new Date().getTime();
    //默认参数
    var defaults = {
        type: 'GET',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        dataType: 'json',
        jsonp: 'callback',
        async: true,
        cache: true,
        ifModified: true,
        // xhrFields: {
        //     withCredentials: true
        // },
        // crossDomain: true,
        title: 'defaultTitle',
        timeout: 30000,
        headers:{},
        beforeSend: function(){  
        },
        error: function () {
        },
        success: function () {
        },
        complete: function(){ 
        },
    };
    //与传入参数合并
    var settings = $.extend({}, defaults, options);
    //判断是否是jsonp
    if (settings.dataType == "jsonp") {
        if (settings.url.indexOf('?') >= 0)
            settings.url = settings.url + "&callback=?";
        else
            settings.url = settings.url + "?callback=?";
    }
    //所有get请求非easyui的datagird请求都添加时间戳
    if (settings.type.toLowerCase() == "get") {
        if (settings.url.indexOf('?') >= 0){
            settings.url = settings.url + "&timestamp=" + timestamp;
        }else{
            settings.url = settings.url + "?timestamp=" + timestamp;
        }
    }
    //开始执行ajax
    $.ajax({
        type: settings.type,
        dataType: settings.dataType,
        contentType: settings.contentType,
        async: settings.async,
        // jsonp: settings.jsonp,
        cache: settings.cache,
        ifModified: settings.ifModified,
        // xhrFields: settings.xhrFields,
        // crossDomain: settings.crossDomain,
        url: settings.url,
        data: settings.data,
        timeout: settings.timeout,
        headers: settings.headers,
        beforeSend: function(XMLHttpRequest){  
            if (typeof settings.beforeSend === "function") {
                settings.beforeSend(XMLHttpRequest)
            };
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) { //失败
            if (typeof settings.error === "function") settings.error(XMLHttpRequest, textStatus, errorThrown);
        },
        success: function (data,textStatus) { //成功
            if (typeof settings.success === "function") {
                if (data == '{}') {
                    data = null;
                }
                if (settings.dataType == 'json') {
                    if (typeof data === "object" && JSON.stringify(data) == '{"error":"session invalidated"}') {
                        top.location.href = '/login';
                    }
                }else{
                    if (data == '{"error":"session invalidated"}') {
                        top.location.href = '/login';
                    }
                }
                settings.success(data,textStatus);
            }
        },
        complete: function (XMLHttpRequest,textStatus){
            if (typeof settings.complete === "function") {
                settings.complete(XMLHttpRequest,textStatus);
            }
        }
    });
}
return Ajax(options);
}

调用方式

var ajax = {
        type:'GET',
        url:data_statistics_service + get_info_list_url,
        dataType:'json',
        data:{},
        error:function(XMLHttpRequest,textStatus,errorThrown) { //失败
            $.messager.alert('提示',$.parseJSON(XMLHttpRequest.responseText).message, 'info');
        },
        success:function(data,valStatus) {
            if (data.code ==0) {
                
                }
            }else{
                
            }
        }
    };
    agent(ajax);

相关文章

网友评论

      本文标题:AJAX封装

      本文链接:https://www.haomeiwen.com/subject/wpfokctx.html