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

Posted random_lee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用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‘;
        }
    }
});

  

以上是关于利用ajax全局设置实现拦截器的主要内容,如果未能解决你的问题,请参考以下文章