Home > Web Front-end > JS Tutorial > body text

How to set request headers in vue-resouce

php中世界最好的语言
Release: 2018-04-18 09:37:43
Original
2272 people have browsed it

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
    }
  }
})
Copy after login

in the Vue instance Second type: Set request header globally

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

The third method: Set

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

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;
 });
})
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!