我尝试使用 axios 发出 GET 请求,但总是收到 401。仅当我从 React 应用程序发送请求时才会发生这种情况。
axios.get('http://localhost:8080/vehicles', {
withCredentials: true,
auth: {
username: 'admin',
password: 'admin'
},
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
使用相同凭据和基本身份验证集的 Postman GET 请求有效。
如果我只需在浏览器中输入网址,就会出现一个登录框,并且使用相同的凭据,它就可以工作,我得到了我想要的。
我也尝试过使用 SuperAgent:
Request.get("http://localhost:8080/vehicles")
.withCredentials()
.auth('admin', 'admin')
.then(response => {
return response.body
});
依然没有。
我看到一个与此相关的问题,但没有人回答。
(基本身份验证:在 supergaent+OSX 上失败,在 superagent+Redhat 上成功,在 Postman+OSX 上成功, https://stackoverflow.com/questions/40064018/basic-authentication-failure-supergaentosx-success-on-superagentredhat-s)
有人能指出我正确的方向吗?
感谢您的阅读。