首页 > 解决方案 > 总是收到 401 错误(“未经授权”)与 axios 对 strapi api 的请求

问题描述

我刚刚遇到了我的 react + strapi 应用程序的问题。我使用的是较旧的节点版本,一切都很好,但现在我重新安装node.js到较新的版本并制作了新的 strpi 项目,制作了相同的表、权限以及与我以前的 strpi 项目相匹配的所有内容。我的反应项目保持不变,我现在使用 axiospostget结果,但现在重新安装后我总是得到POST http://localhost:1338/auth/local 401 (Unauthorized)POST http://localhost:1338/users/ 401 (Unauthorized)错误。注意我使用 1338 端口,事实并非如此。

这是Register.js,我什至不需要身份验证来注册,Public角色有创建用户的权限。

    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);
      });
    }

这是 Login.js,在重新安装之前我可以成功注册然后登录并获取 jwt 令牌

    axios
        .post('http://localhost:1338/auth/local', {
            identifier: this.state.userName,
            password: this.state.userPassword
        })
        .then(response => {
            // Handle success.
            console.log('Well done!');
            // localStorage.setItem('token', response.data.jwt);
            login(response.data.jwt);
            console.log('User profile', response.data.user);
            console.log('User token', AUTH_TOKEN);
        })
        .catch(error => {
            // Handle error.
            console.log('An error occurred:', error.response);
        });

我的项目可能出了什么问题?以前用的很好,有人知道吗?

标签: reactjsapiaxiosstrapihttp-status-code-401

解决方案


推荐阅读