在我的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();
答案 0 :(得分:2)
您确保绑定getSearchResults
,但您没有绑定attemptSearch
。这几乎肯定是问题所在:
this.$search.keyup(this.attemptSearch.bind(this))