jQuery 未捕获的类型错误:无法读取自动完成时未定义的属性“长度”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22706744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 02:03:09  来源:igfitidea点击:

Uncaught TypeError: Cannot read property 'length' of undefined on autocomplete

jqueryjquery-uijquery-ui-autocomplete

提问by xrcwrn

I am trying to make auto complete using the example

我正在尝试使用示例自动完成

for that i am using code

为此,我正在使用代码

<head>
    <title>JSP Page</title>
    <link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
    <script src="js/jquery-1.9.1.js"></script>
    <script src="js/jquery-ui.js"></script>

    <script>

        $(function() {
            function log(message) {
                $("<div>").text(message).prependTo("#log");
                $("#log").scrollTop(0);
            }

            $("#productName").autocomplete({
                source: function(request, response) {
                    alert('hiiiii');
                    $.ajax({
                        url: "ajaxProduct",
                        dataType: "json",
                        data: {
                            qry: $("#productName").val(),
                            maxRows: 5
                        },
                        success: function(data) {
                            alert('success');
                            alert(data.productList);

                            response($.map(data.productList, function(item) {
                                alert(data.productList);
                                console.log(item);
                                return {
                                    label: item.productName,
                                    value: item.productName,
                                    id: item.productId
                                }
                            }));
                        },
                        error: function(data) {
                            alert(data.productList);
                            console.log(typeof data);
                            console.log(data);
                            alert('error');
                        }
                    });
                },
                minLength: 1,
                select: function(event, ui) {
                    log(ui.item ?
                    "Selected: " + ui.item.label :
                    "Nothing selected, input was " + this.value);
                    $("#productId").val(ui.item.id)
                },
                open: function() {
                    $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
                },
                close: function() {
                    $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
                }
            });
        });

    </script>
</head>

<body>
Product Id <span id="productId"></span><br>
Product Name <input type="text" name="productName" id="productName"/>

</body>

It is showing alert('hii');,alert('success');, then in alert box showing undefined.
On console.log it is showing

它显示alert('hii');alert('success');然后在警报框中显示undefined
在 console.log 它显示

event.returnValue is deprecated. Please use the standard event.preventDefault() instead.
Uncaught TypeError: Cannot read property 'length' of undefined jquery-1.9.1.js:766
Denying load of chrome-extension://flhdcaebggmmpnnaljiajhihdfconkbj/jquery-2.0.2.min.map. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension. (index):1

Jquery:766 is

Jquery:766 是

 arg is for internal usage only
    map: function( elems, callback, arg ) {
        var value,
            i = 0,
            length = elems.length,//766 line number
            isArray = isArraylike( elems ),
            ret = [];

How to resolve this.

如何解决这个问题。

回答by Rohan Kumar

Because you are not getting productListin datatry your success functionlike,

因为你不productList进去data试试你success function喜欢的,

success: function(data) {
    if(data.productList){ // check the existance of productList
        alert(data.productList);
        response($.map(data.productList, function(item) {
            console.log(item);
            return {
               label: item.productName,
               value: item.productName,
               id: item.productId
            }
        }));
    }
}

Your response datashould be like,

response data应该像,

{"productList":{"productId": 0, "productName": null} }

回答by Hüseyin BABAL

You have no field name like productListat backend. Use following;

您没有像productList后端那样的字段名称。使用以下;

response($.map(data, function(item) {
    return {
        label: item.productName,
        value: item.productName,
        id: item.productId
    }
}));

回答by coolguy

2.IN your ajaxProduct.php ,you are returning a json encoded array.Make sure it contains the key productList

2.在您的 ajaxProduct.php 中,您将返回一个 json 编码数组。确保它包含密钥 productList

$arr = array('productList' =>'somevalue' ...);

echo json_encode($arr);