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

Using serviceURL with PHP file always results in "Sorry, no matching results"

Open jshster opened this issue 5 years ago • 4 comments

Trying desperately to get the library working with a PHP script. At the moment it's just returning a hardcoded JSON string but as shown in image below ... the result of the PHP file is absolutely 100% a JSON object but the autocomplete field refuses to display it. I know I'm only missing the smallest piece of the puzzle but I'll be darned if I can work out what it is.

devbridge-autocomplete

Here is the instantiation code:

       serviceUrl: 'search-projects.php', //tell the script where to send requests
       dataType: "json",
       minChars: 3,
       width: 450, //set width
       onSelect: function (suggestion) {
           $('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
       },
       transformResult: function (response) {
           console.log(response);  
           return {
               suggestions: $.map(response.myData, function (dataItem) {
                   return {value: dataItem.valueField, data: dataItem.dataField};
               })
           };
       },
       showNoSuggestionNotice: true,
       noSuggestionNotice: 'Sorry, no matching results',
   });

I have posted the same issue on StackOverflow.

jshster avatar Jan 18 '21 07:01 jshster

Check the response format. It should be:

{
  "suggestions": [...]
}

tkirda avatar Jan 18 '21 13:01 tkirda

Hey @tkirda , thanks for the speedy reply. That does indeed solve the issue... but having now re-read the doco the option "transformResult" should, in theory, convert a non-standard response into the "suggestions" response format ... and it clearly didn't do that. Any idea why?

jshster avatar Jan 19 '21 00:01 jshster

Did you provide your transform result function?

tkirda avatar Jan 24 '21 21:01 tkirda

Yep, sure did! Copied it here from the original posting if that helps:

 transformResult: function (response) {
           console.log(response);  
           return {
               suggestions: $.map(response.myData, function (dataItem) {
                   return {value: dataItem.valueField, data: dataItem.dataField};
               })
           };
       },

jshster avatar Jan 25 '21 06:01 jshster