错误在哪里?
我的select2
框未显示从ajax
电话收到的结果,它说 - 无法加载结果。
这是我的代码:
HTML:
<select class="airportList form-control" name="From_1">
<option disabled selected>Going from</option>
</select>
JS:
$(function() {
$(".airportList").select2({
minimumInputLength: 1,
// data: [{id:'ADL',text:'Adelaide, Australia, ADL'},{id:'MEL',text:'Melbourne, Australia, MEL'}],
ajax: {
type: "GET",
url: "data.php",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data) {
return {
results: data
};
}
}
});
});
data.php(我收到):
[
{id:'ADL',text:'Adelaide, Australia, ADL'},
{id:'MEL',text:'Melbourne, Australia, MEL'}
]
答案 0 :(得分:3)
对不起,这是一个更新的答案:
processResults: function(data) {
return {
results: $.map(data, function(obj) {
return {
id: obj.id,
text: obj.text
};
})
};
}
这部分是我想到的问题所在。 请参阅此示例:http://jsfiddle.net/jes0wrka/
答案 1 :(得分:0)
所以我的问题是来自data.php文件的JSON
的格式是INCORRECT !!!:
[
{id:'ADL',text:'Adelaide, Australia, ADL'},
{id:'MEL',text:'Melbourne, Australia, MEL'}
]
CORRECT JSON应该是:
[
{"id":"ADL","text":"Adelaide, Australia, ADL"},
{"id":"MEL","text":"Melbourne, Australia, MEL"}
]