How to completely remove the default query string used in jQuery Autocomplete

I am having trouble trying to figure out how to use jQuery autocomplete in the following situation.

I am trying to connect it to a web service that returns a JSON object. The URL form is as follows:

host/api/schools/{search-term} 

No query string ?term= or anything else.

How can I configure Autocomplete for this? I am new to JavaScript, but I can get through.

I tried the following bit:

 $(...).autocomplete({ source: function(term, callback) { $.getJSON("url", { foo: term }, callback); } }); 

but cannot pass it to the server host/api/schools/{search-term} (it is host/api/schools/?foo%5Bterm=%5D{search-term} as host/api/schools/?foo%5Bterm=%5D{search-term} when checking with a script where {search-term} - entered text.

reference

+4
source share
1 answer

Adding this here, since it basically answers the question, from the jquery-ui documentation:

The third option, callback, provides maximum flexibility and can be used to connect any data source to autocomplete. The callback receives two arguments:

A request object with a single property called "term" that refers to the value currently in the text input. For example, when a user entered "summer time" in the city field, the term Autocomplete will be equal to "new yo"

A response callback that expects a single argument to contain the data offered to the user. This data should be filtered based on the provided term and can be in any of the formats described above for simple local data (String-Array or Object-Array with label / value / both properties). This is important when providing a custom source callback to handle errors during a request. You should always call a response callback, even if you encounter an error. This ensures that the widget always has the correct state.

+1
source

Source: https://habr.com/ru/post/1386393/


All Articles