简体   繁体   中英

Returning results for bootstrap typeahead?

im trying to return results for the bootstrap typeahead after doing an ajax request, but its not working however it does work when i don't initiate the ajax request.

This does not work:

$(".typeahead").typeahead({
  source: function(query, process) {
    return $.ajax({
      url: "/typeahead",
      type: "GET",
      data: "action=" + query,
      success: function(result) {
        return result;  // this returns an array checked with console
      }
    });
  }
});

with no ajax it works:

  $(".typeahead").typeahead({
      source: function(query, process) {
          return ["option1", "option2", "option3"]
      }
    });

You should return process(result) instead of just returning the result.

$(".typeahead").typeahead({
  source: function(query, process) {
    return $.ajax({
      url: "/typeahead",
      type: "GET",
      data: "action=" + query,
      success: function(result) {
        return process(result);  // this returns an array checked with console
      }
    });
  }
});

set the ajax call in one function..

function typeahead()
    {
    $.ajax({
          url: "/typeahead",
          type: "GET",
          data: "action=" + query,
          success: function(result) {
            return result;  // this returns an array checked with console
          }
        });
    }

after initilze u put the code like this

$(".typeahead").typeahead({
      source: function(query, process) {
         var data=typeahead(query);
         process(data);

      }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM