[英]jQueryUI Autocomplete error: this.source is not a function
I am trying to implement a live search function which searches through keys in a JSON fetched from a public api and I am using Jquery UI in order to do so however I am getting the following error, and I am not sure how to solve this error.我正在尝试实现一个实时搜索功能,该功能搜索从公共 api 获取的 JSON 中的键,我正在使用 Jquery UI 来执行此操作,但是我收到以下错误,我不知道如何解决此错误.
Uncaught TypeError: this.source is not a function未捕获的类型错误:this.source 不是函数
<div class="live_search">
<label for="search_player">Search a player </label>
<input id="search_player">
</div>
var request = new XMLHttpRequest()
var data;
request.open('GET', 'https://cors-anywhere.herokuapp.com/https://fantasy.premierleague.com/api/bootstrap-static/', true)
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
data = JSON.parse(this.response);
$(function() {
data;
$("#search_player").autocomplete({
source: data.elements.first_name
});
});
}
console.log(data);
}
request.send();
Any ideas as to what is causing this error?关于导致此错误的原因的任何想法?
If you check the documentation for the autocomplete source
option you can see the issue is because you're providing a single string value.如果您查看自动完成source
选项的文档,您会发现问题是因为您提供的是单个字符串值。
When providing a string it needs to be a valid URL so that the plugin can make a request to that endpoint to retrieve the data to fill the control with.提供字符串时,它需要是一个有效的 URL,以便插件可以向该端点发出请求以检索数据以填充控件。
As the string you're providing is not a valid URL or an array, it's assumed that it's a function.由于您提供的字符串不是有效的 URL 或数组,因此假定它是一个函数。 Hence trying to invoke a string as though it's a function causes the error you see.因此,尝试将字符串当作函数来调用会导致您看到的错误。
To fix this you need to provide a value to source
in a format that it expects, as outlined by the documentation link above.要解决此问题,您需要以预期的格式为source
提供一个值,如上面的文档链接所述。
In addition, you can remove the document.ready event handler you define in the onload
function, as it's redundant, and also the single line with data;
此外,您可以删除您在onload
函数中定义的 document.ready 事件处理程序,因为它是多余的,以及带有data;
的单行data;
as it doesn't do anything.因为它什么都不做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.