Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax / Jquery Autocomplete with JSON data
    primarykey
    data
    text
    <p>I am trying to setup my Jquery UI autocomplete field to have data from an ajax connection. Here is my code so far:</p> <pre><code> $("#mainIngredientAutoComplete").autocomplete({ source: function (request, response) { $.ajax({ url: "../api/IngredientChoices", dataType: "json", success: function (data) { response(function (item) { return { label: item.MainName, value: item.MainItemID } }); } }); } }); </code></pre> <p>This is my JSON:</p> <pre><code>[{"SubItemID":1,"MainItemID":1,"SubName":"2%","MainName":"Milk"},{"SubItemID":2,"MainItemID":1,"SubName":"Skim/Fat Free","MainName":"Milk"},{"SubItemID":3,"MainItemID":2,"SubName":"Chedder","MainName":"Cheese"}] </code></pre> <p>HTML:</p> <pre><code>&lt;table id="tbl_ingredients" style="padding:0px;"&gt; &lt;tr id="ingHeader"&gt; &lt;td&gt;Ingredient&lt;/td&gt; &lt;td&gt;Measurement&lt;/td&gt; &lt;td&gt;Amount&lt;/td&gt; &lt;td&gt;&lt;input id="mainIngredientAutoComplete" /&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>When I start to type "mil" (for milk) my code gives me this error:</p> <p><img src="https://i.stack.imgur.com/BXGSa.png" alt="enter image description here"></p> <p><strong>EDIT:</strong></p> <p>I made your change, which worked for a few attempts but now I am getting a new error - </p> <p>Unhandled exception at line 55, column 25 in [URL]</p> <p>0x800a1391 - Microsoft JScript runtime error: 'data' is undefined</p> <pre><code> $("#mainIngredientAutoComplete").autocomplete({ source: function (request, response) { $.ajax({ url: "../api/IngredientChoices", dataType: "json", response: ($.map(data, function(v,i){ return { label: v.MainName, value: v.MainItemID }})) }); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload