在axios向后端传参时需要设置请求头,确保请求参数的格式为json字符串(此时用json.stringify(obj)无效时)
this.$axios({
method:'',
url:'',
headers: {
'content-type': 'application/json',//设置请求头请求格式为json
'access_token': this.token //设置token 其中k名要和后端协调好
},
params:{}
}).then((response)=>{})
下面看下axios设置请求头内容
axios设置请求头中的authorization 和 cookie 信息:
get请求
axios.get(urlstring,
{
headers: {
'authorization': 'bearer ' + token,
"cookie" : 'sessionid=' + sessionid + '; recid=' + recid,
...
},
params: {
param1: string,
param2: string
},
...
}
)
.then(res => fn)
.catch(e => fn)
post请求
axios.post(urlstring,
{
data: data,
...
},
{
headers: {
'authorization': 'bearer ' + token,
"cookie" : 'sessionid=' + sessionid + '; recid=' + recid,
...
}
}
)
.then(res => fn)
.catch(e => fn)
总结
以上所述是小编给大家介绍的vue-axios 设置请求头问题,希望对大家有所帮助
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!