weixin_33695082 2014-07-25 03:36 采纳率: 0%
浏览 44

Select2 AJAX与JSON

I have been trying to populate my input with select2 using the JSON provided. Here's the JSON:

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

And the html code:

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

And the js code:

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

There's no error in console, but whether I try to input them it's always saying that "searching failed" or there's not even anything. The data from json never showed. Do you have anything to fix this around? Thanks's before :)

  • 写回答

1条回答 默认 最新

  • 7*4 2014-07-25 03:46
    关注

    You have a minor error in your 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 }; 
                }
            }
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码求涡量
  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00
  • ¥15 odbc代码新增sqlserver数据源