jQuery用户界面自动完成不显示结果

jQuery用户界面自动完成不显示结果

问题描述:

我知道问题的标题似乎是一个重复的,但我一直无法找到这个问题的答案。

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

我使用jQuery UI的自动完成,我可以看到正确的JSON数据回来在我的调试器。然而,什么也没有回来的文本框。

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.

我的javascript:

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>

从我AutoCompletePage.aspx页面的代码段:

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);
}

和后...

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

您可以在这张图片中看到,JSON数据的被返回的,但什么也没有发生在我的文本框。在此先感谢任何人谁可以提供帮助。

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.

我中有你不应该使用 JSONP 这里的预感。 JSONP一般用于跨域请求。

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

看样子,你正在在同一个域中(另外,回来的数据可能不会有一个回调函数来调用)的请求,所以你应该罚款只是使用普通的 JSON

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.

试着改变你的数据类型参数 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]}
                    }))
                }
            })
        }
    });