Home  >  Article  >  Web Front-end  >  How to set request headers in vue-resouce

How to set request headers in vue-resouce

php中世界最好的语言
php中世界最好的语言Original
2018-04-18 09:37:432148browse

This time I will show you how to set the request header in vue-resouce. What are the precautions for setting the request header in vue-resouce. The following is a practical case, let's take a look.

The first one: Set

var vm = new Vue({
  el:'#rrapp',
  data:{
   showList: true,
   title: null
  },
  http: {
    root: '/',
    headers: {
      token: token
    }
  }
})

in the Vue instance Second type: Set request header globally

Vue.http.headers.common['token'] = 'YXBpOnBhc3N3b3Jk';

The third method: Set

Vue.http.interceptors.push((request, next) => {
    request.headers.set('token', token); //setting request.headers
    next((response) => {
      return response
   })
})

in the interceptor Attached is vue interceptors to set request headers

Add filter to main.js, you can

Vue.http.interceptors.push((request,next)=>{
 //request.credentials = true; // 接口每次请求会跨域携带cookie
 //request.method= 'POST'; // 请求方式(get,post)
 //request.headers.set('token','111') // 请求headers携带参数
 next(function(response){
  return response;
 });
})

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:



The above is the detailed content of How to set request headers in vue-resouce. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn