ajax 拦截器设置请求头
使用vue-resource时,往headers里添加token后,post方法会自动变成options?
Vue.http.interceptors.push(function(request, next) { // modify method request.method = 'POST'; // modify headers request.headers.set('X-CSRF-TOKEN', 'TOKEN'); request.headers.set('Authorization', 'Bearer TOKEN'); // continue to next interceptor next(); });
放在main.js 文件里面
Vue.http.interceptors.push(function(request, next) {
// modify headers
request.headers.set('Accept', 'application/json');
request.headers.set('Authorization', 'Bearer '+ JSON.parse(localStorage.getItem('token')));
// continue to next interceptor
next();
});