如何格式化 Json 以实现 Jquery 的自动完成
我正在使用 RoR 和 Jquery 编写一个自动完成字段。
当我调用 getJson
:
var aeropuertos;
$.getJSON('web_services/get_airports', function(data) {
aeropuertos = data;
});
$("#airports_input_origen").autocomplete({source:aeropuertos});
$("#airports_input_destino").autocomplete({source:aeropuertos});
我得到这个:
[
[
"(GKA)",
"GOROKA",
"GOROKA",
"PAPUA NEW GUINEA"
],
[
"(LAE)",
"LAE",
"PAPUA NEW GUINEA",
"00"
],
[
"(MAG)",
"MADANG",
"MADANG",
"PAPUA NEW GUINEA"
],
[
"(HGU)",
"MOUNT HAGEN",
"MOUNT HAGEN",
"PAPUA NEW GUINEA"
],
[
"(LAE)",
"NADZAB",
"NADZAB",
"PAPUA NEW GUINEA"
]
]
但似乎格式不正确,因为自动完成功能不起作用并抛出此错误:
this.source 不是一个函数
----------------- 已解决 --------------
它是一个 JSON格式问题!
而不是发送 [ ["", "" , ...] , ... ]
我必须发送:[ "......." , ".........", .. ..]
(解决方案:创建一维 Ruby 的 JSON )
无论如何,谢谢您的时间! =)
I'm writting an Autocomplete field using RoR and Jquery.
When I call getJson
:
var aeropuertos;
$.getJSON('web_services/get_airports', function(data) {
aeropuertos = data;
});
$("#airports_input_origen").autocomplete({source:aeropuertos});
$("#airports_input_destino").autocomplete({source:aeropuertos});
I get this:
[
[
"(GKA)",
"GOROKA",
"GOROKA",
"PAPUA NEW GUINEA"
],
[
"(LAE)",
"LAE",
"PAPUA NEW GUINEA",
"00"
],
[
"(MAG)",
"MADANG",
"MADANG",
"PAPUA NEW GUINEA"
],
[
"(HGU)",
"MOUNT HAGEN",
"MOUNT HAGEN",
"PAPUA NEW GUINEA"
],
[
"(LAE)",
"NADZAB",
"NADZAB",
"PAPUA NEW GUINEA"
]
]
But it seems that the format isn't right since the autocomplete is not working and is throwing this error:
this.source is not a function
----------------- SOLVED --------------
It was a JSON format Issue!
Instead of sending [ ["", "" , ...] , ... ]
I had to send: [ "......." , ".........", ....]
(solution: Create JSON of one dimension Ruby)
Thx anyway for your time! =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论