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();
});

posted @ 2017-03-16 14:12  winyh  阅读(2215)  评论(0编辑  收藏  举报