我试图使用Vue和Axios的GET请求从我们的业务服务器获取一些数据。然而,我遇到了一个401未经授权的错误。当我登录到我的公司帐户时,我就可以用RESTED获得这些数据。我已经看过这篇文章了:How to send Basic Auth with axios,但没有一个解决方案对我有效。这是我发出get请求的代码:
await axios.get('http://192.168.*******', {}, {
auth: {
username: 'username',
password: 'password'
},
headers: {
'Content-Type': 'application/json'
}
});
}
我也尝试过没有“标题”。这是错误消息:
xhr.js:214 GET http://192.******** 401 (Unauthorized)
dispatchXhrRequest @ xhr.js:214
[Vue warn]: Unhandled error during execution of created hook
at <App>
createError.js:19 Uncaught (in promise) Error: Request failed with status code 401
at createError (createError.js:19:15)
at settle (settle.js:19:12)
at XMLHttpRequest.onloadend (xhr.js:75:7)
希望有人有个主意,因为我不知所措。
发布于 2022-03-23 06:22:28
这个解决方案对我有效:
const config = {
'Content-Type': 'application/json',
"Accept": "application/json",
}
const { data } = await axios({ url: 'http://192.168.170.*****/', method: 'GET', data: null, withCredentials: true, headers: config })
https://stackoverflow.com/questions/71352811
复制