[英]I am facing an error net::ERR_ABORTED 401 (Unauthorized)
In my componentDidMount
function I have a problem.在我的
componentDidMount
函数中,我遇到了问题。
I'm facing error 401
in my Reactjs site, my username and password are correct.我的 Reactjs 站点遇到
error 401
,我的用户名和密码正确。
componentDidMount() {
console.log("Json Datalog") fetch('185.213.180.129:9900/cgi-bin/datalog.json', {
'mode': 'no-cors'
}, {
method: "GET",
credentials: 'include',
headers: {
'Authorization': 'Basic ' + btoa(this.state.deviceSettings.userName + ":" + this.state.deviceSettings.password),
}
}).then((response) => {
console.log("Btoa Encryption::::: ", btoa(this.state.deviceSettings.userName + ":" + this.state.deviceSettings.password))
You're passing three arguments to fetch您正在传递三个参数来获取
effectively you are using fetch like您实际上正在使用 fetch 之类的
fetch('185.213.180.129:9900/cgi-bin/datalog.json', {
mode: 'no-cors'
})
So your authentication header is not set at all所以你的身份验证标头根本没有设置
try this instead试试这个
fetch('185.213.180.129:9900/cgi-bin/datalog.json', {
mode: 'no-cors',
method: "GET",
credentials: 'include',
headers: {
'Authorization': 'Basic ' + btoa(this.state.deviceSettings.userName + ":" + this.state.deviceSettings.password),
}
})
also, you need to understand what mode: 'no-cors'
means - you will not have access to the response body if the request is not same origin - so forget reading the response as per the code in your comments (ie return response.json()
) - you will not get any access to the response using mode: 'no-cors'
此外,您需要了解什么
mode: 'no-cors'
意味着 - 如果请求不是同一来源,您将无法访问响应正文 - 所以忘记按照评论中的代码阅读响应(即return response.json()
) - 您将无法使用mode: 'no-cors'
访问响应
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.