[英]How route works in react.js
我正在开发一个项目,我想在从 Ajax 成功登录后加载另一个组件或模块,我们可以使用 react 吗?
$.ajax({
url: url_root+"/auth/token",
dataType: 'json',
type: method.post,
data: data,
success: function(data) {
console.log(data);
// redirect... to another page
}.bind(this),
error: function(xhr, status, err) {
console.error(status);
this.setState({username : true, password :true});
}.bind(this)
});
您想为此使用 ReactRouter。 https://github.com/rackt/react-router
你可以在这里使用承诺。 看看 axios https://github.com/mzabriskie/axios 。 您可以拨打电话,但会返回一个承诺,因此您可以执行以下操作(来自示例...)
axios.get('/user?ID=12345')
.then(function (response) {
this.setState({...)} /// or if you are using Flux (from store emit event to update your view or so on...)
})
.catch(function (response) {
console.log(...)//throw new Error.....
});
应该和使用 axios 一样简单。 存在其他可能性,请参阅http://www.sitepoint.com/comparison-javascript-http-libraries/
希望那有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.