繁体   English   中英

在jQuery的$ .post中调用函数时,未定义不是对象

[英]Undefined is not an object, when calling function inside jQuery's $.post

在我的trySearch函数内部,我使用jQuery $ .post获得一些JSON结果。 我遇到了错误

未定义不是对象

在通话中

this.getSearchResults.bind(本)

我在另一个Web应用程序中设置了相同的设置,但没有收到此错误。 我究竟做错了什么?

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();

您确保绑定了getSearchResults ,但没有绑定attemptSearch 这几乎可以肯定是问题所在:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM