2016-11-05 45 views
1

在我的attemptSearch函數中,我使用jQuery $ .post來獲取一些JSON結果。我發現了錯誤

未定義是不是在通話的對象

this.getSearchResults.bind(本)

我有同樣的設置在另一個網絡應用程序,我不會得到這個錯誤。我究竟做錯了什麼?

var app = { 
    init: function() { 
     this.cacheDom(); 
     this.bindEvents(); 
    }, 
    cacheDom: function() { 
     this.$search = $('#search'); 
    }, 
    bindEvents: function() { 
     this.$search.keyup(this.attemptSearch) 
    }, 
    searchResults : [], 
    getSearchResults : function(val){  
     var currentSearchResult = val.query.search; 
     for (var i = 0; i < currentSearchResult.length; i++) { 
      var result = { 
       title: currentSearchResult[i].title, 
      } 
      console.log(this.searchResults); 
      this.searchResults.push(result); 
     } 
    }, 
    attemptSearch: function(event) { 
     var wiki = "https://crossorigin.me/https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch="; 
     if (event.keyCode == 13) { 
      $.post((wiki + $('#search').val()), this.getSearchResults.bind(this)) 
     } 
    }, 
}; 

app.init(); 

回答

2

你確信綁定getSearchResults,但你沒有綁定attemptSearch。這幾乎肯定是問題:

this.$search.keyup(this.attemptSearch.bind(this)) 
+0

謝謝JLRishe,這的確是問題。 –

相關問題