2016-08-13 84 views
0

我想通过ajax获取json,如果我的响应变量是可以接受的使用react路由器重定向。我怎样才能做到这一点?ReactJs重定向状态变量

successRedirect(){ 
    if (this.responseCode.equals("what I need")) { 
     router.transitionTo('/') 
    } 
} 

createCheckout() { 
    $.ajax({ 
     url: "someurl", 
     dataType: 'json', 
     type: 'POST', 
     cache: false, 
     data: this.data, 
     success: function(response) { 
      this.setState({ 
       response: response, 
       responseCode: response.result.code 
      }); 
     }.bind(this), 
      error: function(xhr, status, err) { 
       console.error(this.props.url, status, err.toString()); 
     }.bind(this) 
    }) 
} 

功能必须在做出响应后调用。例如,我有这样的代码,并在呈现响应取并在一段时间后显示:

render(){ 
    return (
     <div> 
      <div>Response - {this.state.responseCode}</div> 
     </div> 
    ); 
} 
+0

我不明白这一点..所以调用'success'功能? – azium

+0

我得到了undefind this.responseCode – TeodorKolev

+0

你是不是指'this.state.responseCode'? – azium

回答

1

它出现的问题是这一行:

if (this.responseCode.equals("what I need")) { 
是那些获得通过 this.setState添加

项目可在this.state对象。此外JavaScript不提供一个equals功能,但你可以做的比较与===

if (this.state.responseCode === "what I need") {