开发者

handling ajax data with jQuery

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

data from /rooms.json?q=a looks like below:

[{
    "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"
    }
}]

In the above code, I see the first alert but not the second alert so something is going wrong in the handling of the data from the json request.

What should I do to fix this?

Update After playing around w/ some code I figured it out.

I had to tweak the rails controller to render json as below

format.json {render :json => @even开发者_开发百科ts.map(&:attributes)}

Furthermore, for jQuery code I had to remove .room from the code below

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


silly me

in rails code:

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

in jQuery code

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜