2015-09-25 181 views
1

我的完整代碼是http://pastebin.com/xwJ2zi2y。我試圖做的是,當Ajax的refresh_token函數值的值改變時,應該調用order_list函數。當我刷新頁面時,它會給出order_list函數的輸出一次,但是當它開始間隔refresh_token時,它會在pastebin代碼中出現錯誤「Uncaught TypeError:Can not read property'token of undefine」on line 94。並且沒有運行order_list函數,當refresh_token的值發生變化時。 在此先感謝。未捕獲TypeError:無法讀取未定義的屬性'令牌'

回答

-1

shouldComponentUpdate幫我解決了這個問題。在渲染函數之前請參閱下面的代碼。

shouldComponentUpdate:function(nextProps,nextState){return true; },

我的完整運行代碼是http://pastebin.com/xwJ2zi2y。 謝謝

0

您需要在成功回調中存儲用法的上下文。這裏你的固定功能:

refresh_token : function(){ 
        var self = this; 
        $.ajax({ 
         type: 'GET', 
         url: "/order/refresh", 
         headers: { 
         Accept : "application/json", 
         "Content-Type": "application/json" 
         }, 

         success: function (resp){ 
           var newToken = resp; 
            console.log(newToken); //it give the value of refresh eg. ["20150925313"] 

           if(newToken != self.state.token){ 

            self.setState({ token: newToken}); 
              //  console.log(this.state.resp); 
           } 
           } 
        }); 
       }, 
+0

非常感謝你MysterX,這解決了這個錯誤。但是當令牌值改變時,我仍然無法調用Orderlist組件。 –

+0

嗨MysterX,非常感謝你的幫助,你能幫我找出爲什麼我的代碼不會調用orderlist函數時令牌值的變化。 –

+0

@Amit,對不起,但我沒有反應,也沒有研究它,所以我不能給你更多的幫助 – MysterX

相關問題