【问题标题】:Uncaught TypeError: Cannot read property 'length' of undefined JQUERY autocomplete未捕获的类型错误:无法读取未定义 JQUERY 自动完成的属性“长度”
【发布时间】:2015-04-27 16:26:55
【问题描述】:

使用此脚本, https://www.devbridge.com/sourcery/components/jquery-autocomplete/

我正在使用 jQuery Autocomplete 从我的数据库中搜索用户。

下面是返回 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 输入中看不到这个搜索值作为下拉列表:

未捕获的类型错误:无法读取未定义的属性“长度”

这是怎么回事?

谢谢!

【问题讨论】:

标签: javascript jquery json autocomplete


【解决方案1】:

文档解释了“来自服务器的响应必须是 JSON 格式的 JavaScript 对象:”

{
    // 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" }
    ]
}

你只是返回一个没有格式值/数据的数组。您可以在 PHP 中更改格式或使用“transformResult”函数为您的数组创建建议属性。

你应该在你的 PHP 代码中添加一个对象:

$obj = new stdClass();
$obj->suggestions = $a_json;
echo json_encode($obj);

【讨论】:

  • 我设法用 { "Value": "XX" } 得到它,但仍然有同样的问题,我如何在 PHP 中添加 { "Suggestions" : [...] }?跨度>
  • 你需要创建一个对象。让我更新答案。
  • 你太棒了,它起作用了,但只是一个更正,stdObject() 不起作用,它应该是 $obj = new stdClass();谢谢
【解决方案2】:

您不需要调用 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-22
    • 2013-11-24
    • 2021-05-03
    相关资源
    最近更新 更多