vue-resource

vue-resource怎么设置头部的header

阅读 3.1k
3 个回答

用拦截器设置request

Vue.http.interceptors.push((request, next) => {
    request.headers['xxx'] = xxx;
    next(response => {
        ...
    });
});

Interceptors

Interceptors can be defined globally and are used for pre- and postprocessing of a request. If a request is sent using this.$http or this.$resource the current Vue instance is available as this in a interceptor callback.

Request processing

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

摘自文档 https://github.com/pagekit/vu...
像一楼说的,vue.resource( 2.0后不再更新)
推荐使用 axios https://github.com/mzabriskie...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进