2016-08-13 3 views
0

아약스를 통해 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

나는 this.responseCode을 무효화했다. – TeodorKolev

+0

'this.state.responseCode'를 의미하지 않습니까? – azium

답변

1

문제가이 줄을이었다 나타납니다 this.state 개체 또한 자바 스크립트는 equals 기능을 제공하지 않습니다,하지만 당신은 ... ===

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