简体   繁体   English

jQueryUI 自动完成错误:this.source 不是函数

[英]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.

相关问题 jquery 自动完成 this.source 不是函数错误 - jquery autocomplete this.source is not a function error 实现自动完成时出现“this.source 不是函数”错误 - “this.source is not a function” error while implementing autocomplete 在jquery的自动完成功能中得到错误“ this.source不是一个功能。” - Getting the error in the autocomplete function of the jquery “this.source is not a function.” 错误:未捕获类型错误:this.source 不是 function - Error : Uncaught TypeError: this.source is not a function 未被捕获的TypeError:this.source不是功能coffeescript错误 - Uncaught TypeError: this.source is not a function coffeescript error Ajax的jQuery自动完成问题-TypeError:this.source不是函数 - jQuery autocomplete problems with ajax - TypeError: this.source is not a function this.source 不是函数。 jquery-ui 错误 - this.source is not a function. Error with jquery-ui 使用Bloodhound-this.source的Typeahead js不是一个函数 - Typeahead js with Bloodhound- this.source is not a function 选择并按Enter时自动完成jQueryUI双重调用功能 - Autocomplete jQueryUI double call function on select and press enter 将jqueryui自动完成返回的值传递给另一个函数 - Pass value returned from jqueryui autocomplete to another function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM