Jquery UIオートコンプリートフィールドを設定して、ajax接続からのデータを取得しようとしています。これまでの私のコードは次のとおりです。
$("#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
}
});
}
});
}
});
これは私のJSONです:
[{"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"}]
HTML:
<table id="tbl_ingredients" style="padding:0px;">
<tr id="ingHeader">
<td>Ingredient</td>
<td>Measurement</td>
<td>Amount</td>
<td><input id="mainIngredientAutoComplete" /></td>
<td></td>
</tr>
</table>
「mil」(ミルクの場合)と入力し始めると、コードで次のエラーが発生します。
編集:
私はあなたの変更を行いました、それは数回の試みのために働きました、しかし今私は新しいエラーを受け取ります-
[URL]の55行25列の未処理の例外
0x800a1391-Microsoft JScriptランタイムエラー:「データ」が未定義です
$("#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
}}))
});
}
});