利用ajax全局设置实现拦截器

var token = localStorage.getItem("token");

$.ajaxSetup({
	dataType: "json",
	cache: false,
    headers: {
        "token": token
    },
    xhrFields: {
	    withCredentials: true
    },
    complete: function(xhr) {
        //token过期,则跳转到登录页面
        if(xhr.responseJSON.code == 401){
            parent.location.href = baseURL + 'login.html';
        }
    }
});

  

posted @ 2018-12-07 05:27  random_lee  阅读(9027)  评论(0编辑  收藏  举报