2016-08-13 38 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") {