【发布时间】:2011-10-27 10:24:24
【问题描述】:
这是我的 jquery 函数:
$(function() {
$("#user").autocomplete({
source: 'spiderman/null/users.php',
minLength: 2
});
});
当我开始输入至少 2 个字母时
<input type="text" class="highlight" id="user" name="user"/>
它显示了一个项目列表,上面写着“未定义”。
似乎它是一个无效的源,但我很确定它是有效的,因为我在同一个目录中创建了 include_once,并且源脚本已加载。
这是我的 PHP 代码:
if( !$_GET['term'] ) {
die();
}
$return_arr = array();
$ac_term = "%".$_GET['term']."%";
$query = "SELECT `name` FROM `users` WHERE `name` LIKE :term";
$result = $conn->prepare($query);
$result->bindValue(":term", $ac_term);
$result->execute();
/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$row_array['name'] = $row['name'];
array_push($return_arr, $row_array);
}
/* Toss back results as json encoded array. */
echo json_encode($return_arr);
我也测试了 PHP 代码,它运行良好:至于术语 lol 它确实返回了:
[{"name":"LolShit"},{"name":"Lolipop"},{"name":"Lolo"},{"name":"Lolololololololo"},{"name":"Loll"},{"name":"Pro Lol"}]
【问题讨论】:
-
需要设置json文件类型头吗?
标签: jquery-ui autocomplete undefined