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

时间:2022-09-25 23:59:28

Inside my attemptSearch function, I use jQuery $.post to get some JSON results. I'm getting the error

在我的搜索功能中,我使用jQuery $。发布以获得一些JSON结果。我得到错误

Undefined is not an object

未定义的不是对象

on the call

在电话会议

this.getSearchResults.bind(this)

this.getSearchResults.bind(这)

I have the same set up in another web app and I don't get this error. What am I doing wrong?

我在另一个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();

1 个解决方案

#1


2  

You made sure to bind getSearchResults, but you didn't bind attemptSearch. That's almost surely the issue:

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

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

#1


2  

You made sure to bind getSearchResults, but you didn't bind attemptSearch. That's almost surely the issue:

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

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