2014-07-25 52 views
2

我一直在嘗試使用提供的JSON來使用select2填充我的輸入。 這裏的JSON:Select2與JSON的AJAX

{ 
"airports": 
[ 
    { 
     "id": "1", 
     "code": "AMQ", 
     "city": "Ambon", 
     "country": "Indonesia" 
    }, 
    { 
     "id": "2", 
     "code": "BJW", 
     "city": "Bajawa", 
     "country": "Indonesia" 
    } 
] 
} 

和HTML代碼:

<input class="" type='hidden' value="192" data-init-text='Departing City' name='input' id='depart-airport' style="width: 300px"/> 

而且JS代碼:

$(document).ready(function() { 
    $('#depart-airport').select2({ 
    minimumInputLength: 1, 
    ajax: { 
     url: "http://localhost:4000/api/airports.json", 
     dataType: 'json', 
     results: function (data) { 
     return { results: data}; 
     } 
    } 
    }); 
}); 

有控制檯沒有錯誤,但我是否嘗試輸入他們這是總是說「搜索失敗」或者什麼都沒有。 json的數據從未顯示過。 你有什麼可以解決這個問題嗎?感謝的:)

+0

您是否嘗試過調試與谷歌Chrome「網絡」標籤?或者你可以粘貼你的服務器的代碼?我們需要更多信息來幫助你。 –

+1

看到這個希望它有幫助:http://jsfiddle.net/LUsMb/3538/ –

回答

0

之前,你在你的jQuery有一個小錯誤:

$(document).ready(function() { 
    $('#depart-airport').select2({ 
     minimumInputLength: 1, 
     ajax: { 
      url: "http://localhost:4000/api/airports.json", 
      dataType: 'json', 
      results: function (data) { 
       // You had { results: data }, but your actual information is in data.airports 
       return { results: data.airports }; 
      } 
     } 
    }); 
}); 
+0

現在說它的無法讀取屬性'toUpperCase'的undefined。任何想法來解決這些? –