Jquery UI autocomplete not displaying results

让人想犯罪 __ 提交于 2019-12-11 02:14:15

问题


I know the question title seems like a duplicate, but I've been unable to find an answer to this question.

I'm using Jquery UI's autocomplete, and I can see the proper JSON data coming back in my debugger. However, nothing's coming back to the textbox.

My javascript:

<script type="text/javascript">
    $(document).ready(function () {
        myAutoComplete("#<%= myTxtBox.ClientID %>", "AutoCompletePage.aspx");
    });

    function myAutoComplete(ObjectId, DataURL) {
        $(ObjectId).autocomplete({
            source: function (request, response) {
                $.ajax({ url: DataURL, dataType: 'jsonp',
                    data: { q: request.term, limit: 10 },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item[1], value: item[0], id: item[0]}
                        }))
                    }
                })
            }
        });
    }

</script>

a snippet from my AutoCompletePage.aspx page:

foreach (DataRow dataRow in dataTable.Rows)
{
    string[] cells = new string[] { dataRow[0].ToString(), dataRow[1].ToString() };
    output.Add(cells);
}

And later...

Response.Write(json.Serialize(output));

You can see in this picture that JSON data is being returned, but nothing's happening to my textbox. Thanks in advance to anyone who can help.


回答1:


I have a hunch you should not be using jsonp here. JSONP is generally used for cross-domain requests.

It appears that you are making a request in the same domain (additionally, the data coming back may not have a callback function to call), so you should be fine just using normal json.

Try changing your datatype parameter to json:

    $(ObjectId).autocomplete({
        source: function (request, response) {
            $.ajax({ url: DataURL, dataType: 'json',
                data: { q: request.term, limit: 10 },
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item[1], value: item[0], id: item[0]}
                    }))
                }
            })
        }
    });


来源:https://stackoverflow.com/questions/6823146/jquery-ui-autocomplete-not-displaying-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!