我在Vue项目中使用axios,对我的api的调用之一涉及到POST。我的帖子和获取都要求Authorization使用我的令牌设置标头。所有的get请求都可以正常工作,但是将完全相同的标头放入axios.post403中。
这是我的axios代码:
axios.post('https://my.example.org/myapi/meta?uname=' + uname + '&umetaid=' + post.umeta_id + '&umetavalue=' + post.meta_value, {
withCredentials: true,
headers: { 'Authorization': 'Bearer ' + mytoken }
})
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
})
Run Code Online (Sandbox Code Playgroud)
这始终会导致403错误,并且检查我的请求标头会显示从不发送Authorization标头。如果我更改axios.post为axios.get上述内容(并GET在现有代码的基础上,向我的api代码中添加方法POST,OPTIONS),它将很好地执行。我想我可以这样离开,但我认为GET在确实执行通话时使用通话是一种不好的做法POST。POST与axios 形成请求时,我缺少什么吗?
Axios Post请求假定第二个参数是data,第三个参数是config。
当数据附加到URL中时,Axios Get请求假定第二个参数是config。
您正在url中发送数据,该数据应作为第二个参数(对于POST请求)。
代码应为:
var data = {
'uname': uname,
'umetaid': post.umeta_id,
'umetavalue': post.meta_value
}
var headers = {
withCredentials: true,
headers: { 'Authorization': 'Bearer ' + mytoken }
}
axios.post('https://my.example.org/myapi/meta',data,headers)
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2582 次 |
| 最近记录: |