如何解决简单的“Ajax Autocomplete for jQuery”部署中的错误?

问题描述 投票:0回答:1

我正在尝试基于此处找到的“Ajax Autocomplete for jQuery”在应用程序上构建自动完成搜索功能:https://github.com/devbridge/jQuery-Autocomplete

我正在使用 jQuery v3.4.1

初始化它的代码非常简单,并且初始化得很好:

$('#siteSearch').devbridgeAutocomplete({
    paramName: 'q',
    serviceUrl: '/ajax-autocomplete-search'

});

当我在输入字段中输入字符时,会发送 AJAX http 请求并按预期返回 http 响应。

根据文档,http 响应的 JSON 格式正确:

{“建议”:[{“id”:“166”,“domain_name”:“google.com”},{“id”:“167”,“domain_name”:“facebook.com”}]}

令人沮丧的是,此时我收到了一个我根本无法理解的冗长错误。

如果我输入一个字母,返回 JSON 响应后,我会在控制台中收到以下 JavaScript 错误:

jquery.autocomplete.min.js:142 Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
    at _formatResult (jquery.autocomplete.min.js:142:18)
    at Object.<anonymous> (jquery.autocomplete.min.js:704:25)
    at Function.each (jquery-2.2.4.min.js:2:2861)
    at Autocomplete.suggest (jquery.autocomplete.min.js:693:19)
    at Autocomplete.processResponse (jquery.autocomplete.min.js:857:22)
    at Object.<anonymous> (jquery.autocomplete.min.js:610:34)
    at i (jquery-2.2.4.min.js:2:27151)
    at Object.fireWith [as resolveWith] (jquery-2.2.4.min.js:2:27914)
    at z (jquery-2.2.4.min.js:4:12059)
    at XMLHttpRequest.<anonymous> (jquery-2.2.4.min.js:4:15619)

奇怪的是,如果我输入两个字母,一旦返回 JSON,我会得到稍微不同的错误:

Uncaught TypeError: Cannot read properties of undefined (reading 'toLowerCase')
    at Object.<anonymous> (jquery.autocomplete.min.js:789:55)
    at Function.each (jquery-2.2.4.min.js:2:2861)
    at Autocomplete.findBestHint (jquery.autocomplete.min.js:788:19)
    at Autocomplete.onKeyUp (jquery.autocomplete.min.js:467:26)
    at HTMLInputElement.<anonymous> (jquery.autocomplete.min.js:224:26)
    at HTMLInputElement.dispatch (jquery-2.2.4.min.js:3:7537)
    at r.handle (jquery-2.2.4.min.js:3:5620)

我尝试重新下载 jquery 库,我尝试升级到最新的 jQuery 版本(3.7.1)和较旧的版本(2.2.4),看看是否有帮助,但没有运气。

我尝试从项目中删除除 jquery 和自动完成插件之外的所有其他 javascript。

jquery autocomplete
1个回答
0
投票

终于弄清楚了,我的 JSON 格式虽然有效,但没有使用插件定义的特定值名称(这工作正常大约 2 年,所以我只能假设更新破坏了它,但我没有注意到)。

© www.soinside.com 2019 - 2024. All rights reserved.