自动完成isn';我不能在ajax中工作
我这里有一个带有ajax函数的自动完成脚本。我试图通过ajax从数据库中获取数据,但为什么不起作用 下面是我的代码。。任何帮助都将不胜感激 Html自动完成isn';我不能在ajax中工作,ajax,autocomplete,Ajax,Autocomplete,我这里有一个带有ajax函数的自动完成脚本。我试图通过ajax从数据库中获取数据,但为什么不起作用 下面是我的代码。。任何帮助都将不胜感激 Html <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI Autocomplete - Default functionality</title> <link rel="
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#tags" ).autocomplete({
source: function(request, response) {
$.getJSON("autocomplete.php", { app_cn: request.term }, response);
}
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</body>
</html>
jQuery UI自动完成-默认功能
$(函数(){
$(“#标记”).autocomplete({
来源:功能(请求、响应){
$.getJSON(“autocomplete.php”,{app\u cn:request.term},response);
}
});
});
标签:
Autocomplete.php
<?php
include('connect1.php');
if(isset($_GET["app_cn"])) {
$auto = $mysqli1->real_escape_string($_GET["app_cn"]);
//$selected = $mysqli1->real_escape_string($_GET["selected"]);
$sql = $mysqli1->query("SELECT id,item FROM code WHERE item LIKE '%$auto%' GROUP BY item ORDER BY id");
if($sql)
{
$str = array();
while($row = $sql->fetch_assoc())
{
$str[] = $row['item'];
}
echo json_encode($str);
}
}
?>
在控制台中,如果没有看到任何错误,请查看响应。如果响应为空,则添加:
header('Content-Type: application/json');
就在echo json_编码($str)之前
$.getJSON要求标头为application/json您应该在$.getJSON
中使用成功回调函数,并传递到响应变量中。请尝试以下代码
<script>
$(function() {
$( "#tags" ).autocomplete({
source: function(request, response) {
$.getJSON("autocomplete.php", { app_cn: request.term }, function(data){
response(data);
});
}
});
});
</script>
@用户3462269什么问题???@autocomplete不工作,没有数据显示在textbox@user3462269,您能在firebug中看到ajax响应吗??
$str = array();
while($row = $sql->fetch_assoc())
{
array_push($str, array(
"id" => $row['id'],
"label" => $row['item'],
"value" => $row['item']
));
}
header("Content-type: text/json");
echo json_encode($str);