jQuery-Autocomplete icon indicating copy to clipboard operation
jQuery-Autocomplete copied to clipboard

Ajax call without paramName

Open Kosmonaft opened this issue 9 years ago • 2 comments

Is it possible to call an API without paramName? The reason I'm asking is because the API end point which I'm trying hit is restcountries And query should look like this: https://restcountries.eu/rest/v1/name/xxx xxx- string

I tried in this way but the url is following: https://restcountries.eu/rest/v1/name/?null=ger

$('#search').autocomplete({ paramName:null, serviceUrl: 'https://restcountries.eu/rest/v1/name/', onSelect: function (suggestion) { alert('You selected: ' + suggestion.value + ', ' + suggestion.data); } });

Kosmonaft avatar Jan 27 '17 06:01 Kosmonaft

Here, the ajaxClass need name for parameters

Quick hack

$('#search').autocomplete({
  paramName:'name',
  serviceUrl: 'https://restcountries.eu/rest/v1',
  ajaxSettings:{
  beforeSend : function(jqXHR, settings) {
          settings.url=settings.url.replace("=","/").replace("?","/")
      }
  }
});

But you need to update response too : see https://www.devbridge.com/sourcery/components/jquery-autocomplete/#jquery-autocomplete-non-standard-queryresults

Shnoulle avatar Jan 28 '17 15:01 Shnoulle

@Shnoulle Thank you. I didn't saw beforeSend.

Here is the whole solution if someone is going to need it:

$('#search').autocomplete({
    paramName: 'name',
    serviceUrl: 'https://restcountries.eu/rest/v1',
    minChars: 2,
    ajaxSettings: {
        beforeSend: function (jqXHR, settings) {
            settings.url = settings.url.replace("=", "/").replace("?", "/");
        }
    },
    transformResult: function (response) {
        return {
            suggestions: $.map($.parseJSON(response), function (item) {
                return {
                    value: item.name,
                    data: item
                };
            })
        };
    }
});

Kosmonaft avatar Jan 30 '17 00:01 Kosmonaft