reactjs - 总是收到 401 错误(“未经授权”)与 axios 对 strapi api 的请求
问题描述
我刚刚遇到了我的 react + strapi 应用程序的问题。我使用的是较旧的节点版本,一切都很好,但现在我重新安装node.js
到较新的版本并制作了新的 strpi 项目,制作了相同的表、权限以及与我以前的 strpi 项目相匹配的所有内容。我的反应项目保持不变,我现在使用 axiospost
或get
结果,但现在重新安装后我总是得到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);
});
我的项目可能出了什么问题?以前用的很好,有人知道吗?
解决方案
推荐阅读
- python - 2018年熊猫世界杯数据集净胜球
- python - 如何做numpy.sctype2char的倒退。将 char 转换为 dtypes 或 sctype
- django - 在 Django 中请求数据
- swift - 在 tableView 上搜索多个对象
- angularjs - angularjs - 从下拉列表中选择图像(按钮)
- java - 用于 jdbcTemplate 查询的 Mockito 中的 ClassCastException
- reactjs - 无法使用 nextjs 正确创建主布局
- angular - 添加到购物车在 Angular 中不起作用
- sql - 从没有光标的多个输入中选择行
- python - 将列表设置为 dict 中的键,列表元素数量未知