【发布时间】:2020-03-07 05:08:03
【问题描述】:
我正在向具有相同标头(包括 Bearer)的同一端点发送 GET 请求,但是当我从 Postman 调用时得到 200 和正确的 JSON,当我在我的 (Vue) 中使用 Axios 发送请求时) 项目我得到 302。
来自本地项目的调用,在 localhost:8080 上运行(如果有用的话)如下:
const { authToken, userID } = store.getters.authUser
const config = {
method: 'get',
url: '/' + userID,
headers: {
Authorization: `Bearer ${authToken}`,
'Content-Type': 'application/json'
}
}
axios(config)
.then(res => {
console.log(res)
return res
})
.catch(e => {
console.log(e)
})
在邮递员中,我所做的只是创建一个具有相同 url 的 GET 请求,而我在标头中添加的只是“Bearer ...”
我从 axios 得到的错误是:
Error: Network Error
at createError (createError.js?2d83:16)
at XMLHttpRequest.handleError (xhr.js?b50d:87)
响应状态为 302 ,知道这里发生了什么吗??
userID 和 authToken 的值都存在并且等于我在邮递员中使用的相同值,网址也是如此
【问题讨论】:
-
你是否在标题中设置了 {'Content-Type': 'application/json'}?
-
是的,我已经设置了标题
-
你试过使用网址
http://localhost:8080/${userID}吗? -
我没有针对本地服务器工作...请求转到另一台服务器
-
你试过用服务器的完整地址吗?