【发布时间】:2015-02-05 15:06:46
【问题描述】:
我正在使用 Select2 jquery 插件,但无法使用 json 获得结果。在浏览器中查看 json 响应时,它看起来不错。比如这样:
[{
"id" : "50",
"family" : "Portulacaceae "
}, {
"id" : "76",
"family" : "Styracaceae "
}, {
"id" : "137",
"family" : "Dipsacaceae"
}
]
在这种情况下使用 ajax 调用的 URL 是:http://localhost/webpage/json_family.php?term=acac&_=1417999511783 但我无法在 select2 输入中得到该结果,控制台说:
未捕获的类型错误:无法读取未定义的属性“长度”
代码如下:
html
<input type="hidden" id="select2_family" name="term" style="width:30%" />
js
$("#select2_family").select2({
minimumInputLength: 3,
ajax: {
url: "json_family.php",
dataType: 'json',
data: function (term) {
return {
term: term,
};
},
results: function (data) {
return { results: data.results };
}
}
});
php
$myArray = array();
if ($result = $mysqli->query("SELECT id,family FROM family WHERE family LIKE '%$term%'")) {
$tempArray = array();
while($row = $result->fetch_object()) {
$tempArray = $row;
array_push($myArray, $tempArray);
}
echo json_encode($myArray);
}
代码有错误吗?
【问题讨论】:
-
试试我更新的 php.ini 文件。对插件不够熟悉,无法直接解决这个问题,但我浏览了一些文档。希望你能尽快完成这项工作
标签: javascript php jquery json jquery-select2