5

jQuery UI Autcomplete:

How can I POST the term to the search script instead of GET?

andrhamm
  • 3,924
  • 4
  • 33
  • 46
  • Wouldn't the POST be more appropriately used for obtaining the user's selection, rather than simply retrieving a list of choices? – Robert Harvey Apr 09 '10 at 16:19
  • 2
    @Robert - if receiving back JSON, a post might be better, esp. in ASP.NET MVC2 which (by default) refuses to serve up JSON for a GET request: http://haacked.com/archive/2009/06/25/json-hijacking.aspx – tvanfosson Apr 09 '10 at 16:25

2 Answers2

7

You'll need to supply a function as the source for the plugin and have your function do the AJAX post to the server to get the matching data.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 5
    thanks, this is how it ended up working: $("#search-cities").autocomplete( { minLength: 2, source: function(request, response) { jQuery.post("http://redcupclassifieds.com/search_cities", { term: request.term }, function(data) { response(data); }, "json"); } }); – andrhamm Apr 16 '10 at 16:53
2

You need to specify callback function for the source parameter. Here is an example: http://jqueryui.com/demos/autocomplete/#remote-jsonp

Ivo Sabev
  • 5,230
  • 1
  • 26
  • 38