0

I have a script with autocomplete, get some data from an external source according to searched term. I can output the json in the console but I'm struggling to pass it to the response, how do I do that?

      $('#test').autocomplete({
        source: function(request,response){
            $.post('/schoollookup', {
               query: request.term
            }, function(data){
                }, 'json'
            );
        },
        minLength: 2
    });
Patrice
  • 41
  • 6
  • Hi Patrice, A couple problems I see at a glance, JSON is not an array as the function expect, also there is no return statement in your function. I found the following article which you may find helpful. https://stackoverflow.com/questions/11435433/jquery-ui-autocomplete-with-json. Also, with this being tagged PHP I'm going to guess your data feed is in PHP. I've had issues in the past with JSON when I forgot to assign the proper header in my PHP `header('Content-Type: 'application/json');` – dotParx Oct 25 '18 at 11:19

3 Answers3

0
$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

This is the syntax for post request. where

url : A string containing the URL to which the request is sent.

data : A plain object or string that is sent to the server with the request.

success : callback function

Sumesh TG
  • 2,557
  • 2
  • 15
  • 29
0

@Sumesh

    $.post('/schoollookup', {

should be working the same, the difficulty that I have is to get response

Patrice
  • 41
  • 6
0

Thank you for your answer r007ed, the issue was that it was not returning an array. So the final code for this is :

    $('#test').autocomplete({
        source: function(request,response){
            $.post('/schoollookup',{query: request.term}, response, 'json');
        },
        minLength: 2
    });
Patrice
  • 41
  • 6