reactjs - Axios 上的 React 登录组件
问题描述
如何通过 API 制作 React 登录组件并在提供会话时重定向到主页(通过 Jwt)?
我尝试使用默认的 Axios 功能,但遇到 404 错误,而 API 可以通过 GET 访问
下面是 Login.js
handleSubmit = input => {
this.setState({ loading: true });
input.preventDefault();
if (this.formisValid(this.state)) {
this.setState({ errors: [], loading: true });
axios({
method: 'POST',
url: 'http:localhost:3003/api/v1/login',
data: {
email : this.state.email,
password : this.state.password
}
}).then( user => {
this.setState({
initialState,
submit: true
});
this.setState({ loading: false});
console.log('User Login', user)
}).catch((response) => {
// ? Show to user that request is failed
this.setState({ errors:[response ]})
this.setState({ loading: false });
console.log('request failed', response)
});
}
};
下面是 API GET 登录结果(通过浏览器)
{"success":false,"data":{"error":"Body cannot be empty"}}
预期的结果是用户可以在登录成功后被定向到主页(/)(并通过 Jwt 进行会话)
谢谢你的帮助
解决方案
你能试试这个吗?
axios.post('http://localhost:3003/api/v1/login', {
email : this.state.email,
password : this.state.password
})
.then(user => {
/* */
})
推荐阅读
- python - How to get fields values of a model in xml through Python in qweb report odoo 12?
- qt - How to programmatically select multiple rows in QListView?
- java - Add optional header to WebClient get request
- shell - How to use data of curl -v?
- react-native - 如何映射在 react-native 中进行 Api 调用时返回的数据
- javascript - Decrypt data using CryptoJs
- java - 如何将控制器中的 RequestBody 定义为列表,我收到 500 错误
- mysql - 如何从工作台 SQL 导出和导入数据库
- swagger - how to add Authorization header by using open api swagger doc
- xcode - error while trying to get build ios using xcode