使用 axios 帖子获取 401(未经授权)错误

时间:2021-04-21 14:07:17

标签: reactjs axios

我使用 Reactjs 制作了注册和登录页面。如果我使用 Postman,POST 请求工作正常,但如果我尝试使用 axios 发送请求,我总是收到 401 (Unauthorized) 错误。这是我的注册代码:

axios.post('http://localhost:1338/users/', {
headers: { 'Content-Type': 'application/json'},
username: this.state.userName,
email: this.state.userEmail,
password: this.state.userPassword,
confirmed: true,
blocked: false

})
.then((response) => {
    console.log(response);
  }, (error) => {
    console.log(error);
  });
}

我检查了 state 变量实际上是正确的,我什至尝试将字符串直接硬编码为 usernameemailpassword。但无论如何它都会抛出同样的错误。可能有什么问题?

编辑:

我得到了在浏览器中检查 Request Headers 的建议,当我尝试注册时,我得到两个实例:

enter image description here

我检查了请求标头,发现有 Authorization: Bearer null。但是在注册时我不会发送授权标头。我得到了清除 cookie 和 localstorage 的建议,我该怎么做?

0 个答案:

没有答案