Uncaught TypeError: Cannot read property 'length' of undefined JQUERY autocomplete
Uncaught TypeError: Cannot read property 'length' of undefined JQUERY autocomplete
使用此脚本,
https://www.devbridge.com/sourcery/components/jquery-autocomplete/
我正在使用 jQuery 自动完成功能从我的数据库中搜索用户。
下面是控制器 returns Json:
public function searchusers1() {
if ($_GET) {
$query = $this -> input -> get('query');
$searcharray = $this -> model_usermanage -> searchuser($query);
$a_json = array();
$a_json_row = array();
foreach($searcharray as $row) {
//$user_firstname = htmlentities(stripslashes($row['user_firstname']));
//$user_lastname = htmlentities(stripslashes($row['user_lastname']));
$user_email = htmlentities(stripslashes($row['user_email']));
//$a_json_row["user_firstname"] = $user_firstname;
$a_json_row["user_email"] = $user_email;
array_push($a_json, $a_json_row);
}
echo json_encode($a_json);
}
}
下面是我的jQuery:
$('#reply_bcc').autocomplete({
serviceUrl: '<?php echo base_url(); ?>index.php/hi/test/searchusers1',
minChars: 3,
onSelect: function (suggestion) {
console.log('You selected: ' + suggestion.data + ', ' + suggestion.data);
}
});
这是我的 JSON 回复:
[{"user_email":"hi@test.com"},{"user_email":"hello@au.com"},{"user_email":"testing@123.com"},]
我的HTML:
<div class="col-md-10">
<input type="text" name="reply_bcc" id="reply_bcc" autocomplete="off" class="form-control">
</div>
我的问题是:
我在控制台中遇到以下错误,并且在我的 html 输入中看不到此搜索值作为下拉列表:
Uncaught TypeError: Cannot read property 'length' of undefined
这是怎么回事?
谢谢!
文档解释 "Response from the server must be JSON formatted following JavaScript object:"
{
// Query is not required as of version 1.2.5
"query": "Unit",
"suggestions": [
{ "value": "United Arab Emirates", "data": "AE" },
{ "value": "United Kingdom", "data": "UK" },
{ "value": "United States", "data": "US" }
]
}
而您只是返回一个没有 value/data 格式的数组。您可以在 PHP 中更改格式或使用 "transformResult" 函数为您的数组创建建议 属性。
您应该在 PHP 代码中添加一个对象:
$obj = new stdClass();
$obj->suggestions = $a_json;
echo json_encode($obj);
您不需要调用 stdClass()。函数 json_encode() 为您将关联数组转换为 JSON。这是一个例子:
<?php
$query="";
if (isset($_GET['query'])) $query=$db_handler->real_escape_string($_GET['query']);
$sql="SELECT `user_id`,`user_name` FROM `some_table`
WHERE `user_name` LIKE '%{$query}%' ORDER BY `user_name`";
$res=$db_handler->query($sql);
if ($res->num_rows > 0)
{
$data=array();
while($row = $res->fetch_object())
{
$data[]=array("value" => $row->user_name, "data" => $row->user_id);
}
$res->free();
}
header('Content-Type: application/json');
echo json_encode(array("suggestions" => $data));
?>
上面的代码会输出如下内容:
{"suggestions":[{"value":"Joe Blow","data":"1000"},{"value":"Jane Blow","data":"1001"}]}
这是 .autocomplete 期望的格式:JavaScript Object Notation。
使用此脚本, https://www.devbridge.com/sourcery/components/jquery-autocomplete/
我正在使用 jQuery 自动完成功能从我的数据库中搜索用户。
下面是控制器 returns Json:
public function searchusers1() {
if ($_GET) {
$query = $this -> input -> get('query');
$searcharray = $this -> model_usermanage -> searchuser($query);
$a_json = array();
$a_json_row = array();
foreach($searcharray as $row) {
//$user_firstname = htmlentities(stripslashes($row['user_firstname']));
//$user_lastname = htmlentities(stripslashes($row['user_lastname']));
$user_email = htmlentities(stripslashes($row['user_email']));
//$a_json_row["user_firstname"] = $user_firstname;
$a_json_row["user_email"] = $user_email;
array_push($a_json, $a_json_row);
}
echo json_encode($a_json);
}
}
下面是我的jQuery:
$('#reply_bcc').autocomplete({
serviceUrl: '<?php echo base_url(); ?>index.php/hi/test/searchusers1',
minChars: 3,
onSelect: function (suggestion) {
console.log('You selected: ' + suggestion.data + ', ' + suggestion.data);
}
});
这是我的 JSON 回复:
[{"user_email":"hi@test.com"},{"user_email":"hello@au.com"},{"user_email":"testing@123.com"},]
我的HTML:
<div class="col-md-10">
<input type="text" name="reply_bcc" id="reply_bcc" autocomplete="off" class="form-control">
</div>
我的问题是:
我在控制台中遇到以下错误,并且在我的 html 输入中看不到此搜索值作为下拉列表:
Uncaught TypeError: Cannot read property 'length' of undefined
这是怎么回事?
谢谢!
文档解释 "Response from the server must be JSON formatted following JavaScript object:"
{
// Query is not required as of version 1.2.5
"query": "Unit",
"suggestions": [
{ "value": "United Arab Emirates", "data": "AE" },
{ "value": "United Kingdom", "data": "UK" },
{ "value": "United States", "data": "US" }
]
}
而您只是返回一个没有 value/data 格式的数组。您可以在 PHP 中更改格式或使用 "transformResult" 函数为您的数组创建建议 属性。
您应该在 PHP 代码中添加一个对象:
$obj = new stdClass();
$obj->suggestions = $a_json;
echo json_encode($obj);
您不需要调用 stdClass()。函数 json_encode() 为您将关联数组转换为 JSON。这是一个例子:
<?php
$query="";
if (isset($_GET['query'])) $query=$db_handler->real_escape_string($_GET['query']);
$sql="SELECT `user_id`,`user_name` FROM `some_table`
WHERE `user_name` LIKE '%{$query}%' ORDER BY `user_name`";
$res=$db_handler->query($sql);
if ($res->num_rows > 0)
{
$data=array();
while($row = $res->fetch_object())
{
$data[]=array("value" => $row->user_name, "data" => $row->user_id);
}
$res->free();
}
header('Content-Type: application/json');
echo json_encode(array("suggestions" => $data));
?>
上面的代码会输出如下内容:
{"suggestions":[{"value":"Joe Blow","data":"1000"},{"value":"Jane Blow","data":"1001"}]}
这是 .autocomplete 期望的格式:JavaScript Object Notation。