-1

I am working on a project where I want to load another component or module after successful login from Ajax can we do with 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)
});
Clarkie
  • 7,490
  • 9
  • 39
  • 53
Aatif Bandey
  • 1,193
  • 11
  • 14

2 Answers2

1

You want to use ReactRouter for this. https://github.com/rackt/react-router

lostPixels
  • 1,303
  • 9
  • 23
0

You could use a promise here. Have a look at axios https://github.com/mzabriskie/axios . You can make your call but a promise is returned so you can do something like (from examples...)

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

It should be as simple as that using axios. Other possibilities exist see http://www.sitepoint.com/comparison-javascript-http-libraries/

Hope thats useful

Davet
  • 334
  • 1
  • 3
  • 15