这是我的jQuery函数:自动填充返回undefined
$(function() {
$("#user").autocomplete({
source: 'spiderman/null/users.php',
minLength: 2
});
});
当我开始在我的
<input type="text" class="highlight" id="user" name="user"/>
是表示说:“不确定的”一个项目列表输入最少2个字母。 似乎它是一个无效的来源,但我敢肯定它是有效的,因为我已经在同一目录中制作了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文件类型标头? – Tim
@Tim你在做什么? – Cyclone