开发者

Jquery UI autocomplete not displaying results

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[] { dat开发者_开发知识库aRow[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.

Jquery UI autocomplete not displaying results


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜