jQuery的自动完成远程数据源JSON不返回数据 [英] jQuery autocomplete remote JSON datasource not returning data

查看:124
本文介绍了jQuery的自动完成远程数据源JSON不返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个永远的工作,并通过所有其它的例子搜查,似乎仍不能推测出来,试图用jQuery用户界面自动完成,第一次试图把这个放在一起。这里是我的JS:

I've been working forever on this, and searched through all the other examples and still can't seem to figure it out, trying to use jquery ui autocomplete, first time trying to put this all together. Here is my JS:

$(document).ready(function () {
$("#search-title").autocomplete({
    source: function ( request, response ) {
        $.ajax({
            url: "/include/autocomplete",
            dataType: "json",
            data: {
                        term: request.term      
                    },
            success: function (data) {
                response( $.map( data.stuff, function ( item ) {
                    return {
                        label: item.name,
                        value: item.name
                    };
                }));
            }
        });
    },
    minLength: 2,
    focus: function (event, ui) {
        $(event.target).val(ui.item.label);
        return false;
    },
    select: function (event, ui) {
        $(event.target).val(ui.item.label);
        window.location = ui.item.value;
        return false;
    }
});
});

签出在Firebug的反应,我想我在这里得到正确格式的JSON:

Checking out the Response in Firebug, I think I'm getting properly formatted JSON here:

{"stuff":[ {"label" : "Dragon", "value" : "http://site.com/animal/firebreathers"}] }

但由于某些原因,它不挂钩。我打的minLength后小,空框会打开搜索栏下方,但什么也不会在那里。

But for some reason it's not hooking up. After I hit the minLength a small, empty box will open beneath the search field but nothing will be in there.

更新:当我添加警报(项目);在响应号召,我得到一个说窗在site.com的网页显示:对象的对象? - 这会是问题。

UPDATE: When I add "alert(item);" in the response call, I get a window that says "The page at site.com says: object Object" -- could this be the issue?

推荐答案

我终于找到它了,这要感谢下我的问题那一抹评论。

I finally figured it out, thanks to that hint comment under my question.

在回报,这是我从jQuery UI的网站抄袭,我有:

In the return, which I copied from the Jquery UI site, i had:

label: item.name,
value: item.name

改变,为:

label: item.label,
value: item.value

解决了我与jQuery斗争。我不知道如果这是很好的做法,但最后的作品!

Solved my struggle with jQuery. I have no idea if this is good practice, but it finally works!

这篇关于jQuery的自动完成远程数据源JSON不返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆