封装了jQuery的Ajax请求全局配置

2020-05-22 17:05:11易采站长站整理

        ‘¥’    : ‘¥’,
        ‘¥’   : ‘¥’,
        ‘€’   : ‘?’,
        ‘€’  : ‘?’,
        ‘§’   : ‘§’,
        ‘§’   : ‘§’,
        ‘©’   : ‘©’,
        ‘©’   : ‘©’,
        ‘®’    : ‘®’,
        ‘®’   : ‘®’,
        ‘™’  : ‘™’,
        ‘™’  : ‘™’,
        ‘×’  : ‘×’,
        ‘×’   : ‘×’,
        ‘÷’ : ‘÷’,
        ‘÷’   : ‘÷’
    };
    if (str === null) {
        return ”;
    }
    str = str.toString();
    return str.indexOf(‘;’) < 0 ? str : str.replace(reg, function(chars) {
        return entity[chars];
    });
}
// 转换html的实体
$.ajaxSetup({
    global     : true,
    cache      : false,
    converters : {
        ‘text json’ : function(response){
            return jQuery.parseJSON(unescapeEntity(response));
        }
    }
});
/*
*Ajax 请求权限异常
*   用户权限错误跳转登陆页
*   404错误跳转404页面
 */
$(document).ajaxComplete(function(evt, req, settings){
    if(req && req.responseJSON){
        var json = req.responseJSON;
        if(json.code === 403 && json.info === ‘perm error’ && !json.success){
            window.location.href = location.protocol + ‘//’ + location.hostname;