【问题标题】:handling ajax data with jQuery使用 jQuery 处理 ajax 数据
【发布时间】:2011-05-29 13:21:12
【问题描述】:
$.ajax({
    url: "/rooms.json",
    dataType: "json",
    data: {
        q: req.term
    },
    success: function (data) {
        alert('test');
        responseFn($.map(data.room, function (item) {
            alert('test1')
            return {
                label: item.title
                value: item.title
            }
        }));
    }
});

来自/rooms.json?q=a 的数据如下所示:

[{
    "room": {
        "created_at": "2011-05-19T18:08:04Z",
        "title": "Great Office Space for Rent!",
        "cost": 450,
        "updated_at": "2011-05-19T18:08:04Z",
        "property_id": 4,
        "maximum_capacity": 234,
        "id": 15,
        "fulladdress": "550 12th St NW, Washington D.C., DC 20005, USA",
        "user_id": null,
        "phone": "301-395-7578",
        "description": "Great office space to rent in DC\r\n\r\nPlease contact. ",
        "email": "something@gmail.com"
    }
}]

在上面的代码中,我看到了第一个alert,但没有看到第二个alert,因此在处理来自 json 请求的数据时出现了问题。

我应该怎么做才能解决这个问题?

更新 在玩了一些代码之后,我想通了。

我不得不调整 rails 控制器来渲染 json 如下

format.json {render :json => @events.map(&:attributes)}

此外,对于 jQuery 代码,我必须从下面的代码中删除 .room

responseFn($.map(data.room, function (item) {

【问题讨论】:

  • responseFn() 是 jQuery 函数,还是您定义的函数?另外,检查错误日志。有什么错误吗?
  • responseFn() 是 jQueryUI 自动完成的一部分。在玩了一些调整之后,我想通了。可能在发布问题时跳了枪。我已经用下一个人的答案更新了它。

标签: jquery-ui jquery autocomplete


【解决方案1】:

愚蠢的我

在 Rails 代码中:

format.json {render :json => @days.map(&:attributes)}

在 jQuery 代码中

responseFn( $.map( data, function( item ) {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-13
    • 2011-03-22
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2015-09-26
    相关资源
    最近更新 更多