2016-08-13 12 views
0
私はAJAX経由でJSONを取得しようとしています

にリダイレクトし、私の応答変数がある場合は許容リダイレクトルータを使用して反応させます。それをどうすれば実現できますか?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) 
    }) 
} 

応答を受け取った後に関数を呼び出す必要があります。たとえば、私はこのコードを持っているし、応答をレンダリングするには、いくつかの時間後に撮影して示されている:

if (this.responseCode.equals("what I need")) { 
this.setState経由で追加されます

アイテムで使用できます。

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

私はそれを取得しないとの比較を行うので、 'SUCCESS'で関数を呼び出すことができますか? – azium

+0

私はundefind this.responseCodeを得た – TeodorKolev

+0

'this.state.responseCode'を意味しないのですか? – azium

答えて

1

問題は、この行であった場合に表示されますthis.stateオブジェクトです。また、JavaScriptがequals機能を提供していませんが、あなたは... ===

if (this.state.responseCode === "what I need") { 
関連する問題