-1

我正在做一个项目,我想在从 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)
});
4

2 回答 2

1

您想为此使用 ReactRouter。https://github.com/rackt/react-router

于 2015-09-23T15:10:45.190 回答
0

你可以在这里使用一个承诺。看看 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/

希望那有用

于 2015-10-08T16:00:42.513 回答