axios官网:axios使用说明

axios的安装及相关配置:

     安装:npm install axios

     配置:       

//在Main.js文件中引入:
import axios from 'axios'

//注册到全局:
Vue.prototype.$http = axios; //这样就可以在Vue实例中通过this.$http来使用axios

        post请求方式是:

this.$http({
    url:url,
    method:'post',
    data:data  //注意属性是data
}).then(res=>{
    ....
}).catch(err=>{
    ....
})

get请求方式是:

this.$http({
    url:url,
    method:'get',
    params:params  //注意属性是params
}).then(res=>{
    ....
}).catch(err=>{
    ....
})

 

一、设置默认请求头(如:给每个URL请求都带上存储在客户端cookie中的token、clientId等信息):

//在Main.js中设置默认请求头的参数

if (Cookies.get("token") && Cookies.get("clientId")) {
    //设置字段:Authorization,值为:token
  axios.defaults.headers.common["Authorization"] = Cookies.get("token");
    //设置字段: clientId,值为:clientId
  axios.defaults.headers.common["clientId"] = Cookies.get("clientId");
}

官网对设置默认请求头相关说明

axios的常用用法注解

 

 

 

 

 

 

相关文章: