2016-11-29 42 views
0
handleAddUser(){ 
    if(this.checkRequiredField()){ 
     $.ajax({ 
      url:this.state.location, 
      datatype:'text', 
      type:'POST', 
      data:{ 
       UserId:this.state.userId, 
       AccountName:this.state.accountName 
      }, 
      success:function(data,statuc,xhr){ 
       console.log(xhr.getResponseHeader('Location')); 
       console.log('data added successfully');     
      this.updateUserList(xhr.getResponseHeader('Location'));     
      }, 
      error:function(xhr,status,err){ 
       console.error(status, err.toString()); 
      } 
     }); 
    } 
} 

在上面的post請求中,它的成功函數有一個方法來更新用戶列表。TypeError:this.updateUserList不是函數。 reactjs

updateUserList方法定義並在構造函數中提到這樣

this.updateUserList = this.updateUserList.bind(this); 

,但沒有被執行的方法,而不是我在瀏覽器控制檯

TypeError: this.updateUserList is not a function下面的錯誤。

我在另一個屏幕上有類似的代碼,它按預期工作。 如何解決這個問題?

回答

3

您需要綁定ajax cal的成功函數。因爲你不過this調用updateUserList功能this.updateUserList,成功調用內部並不是指外部範圍,但成功的呼叫

handleAddUser(){ 
    if(this.checkRequiredField()){ 
     $.ajax({ 
      url:this.state.location, 
      datatype:'text', 
      type:'POST', 
      data:{ 
       UserId:this.state.userId, 
       AccountName:this.state.accountName 
      }, 
      success:function(data,statuc,xhr){ 
       console.log(xhr.getResponseHeader('Location')); 
       console.log('data added successfully');     
       this.updateUserList(xhr.getResponseHeader('Location'));     
      }.bind(this), 
      error:function(xhr,status,err){ 
       console.error(status, err.toString()); 
      }.bind(this) 
     }); 
    } 
} 
+0

感謝@Shubham卡特里範圍出現問題。 – MemoryLeak

相關問題