开发者

render the returned data from rails controller via ajax

In rails application, i am trying to fetch few records using a Ajax/jQuery, am getting the right data but the problem how do i parse the returned data and render it as HTML?

my js function

 $.ajax({
        url: "/questions/retrieve_function",
        dataType:'json',
        data: {
            'obsolete': 1
    },
    success: function(data){
        $.each(data, function(key, value) {
            alert(key + ", " + $.param(value));
        });

    },
    error: function(){
        alert('error');
    },
    type: 'GET'
});

my controller method:

def retrieve_f开发者_运维百科unction
 obsolete = params[:obsolete]
 @values = values.find(:all, :conditions => ["obsolete = ?", obsolete])
  respond_to do |format|
   format.html
   format.json { render :json => @values }
  end
end

am using $.param currently to check whether am getting correct values, any help would be appreciated thanks :)

Update

The format returned

"[[
  {\"question\":{\"created_at\":\"2011-09-26T12:56:41Z\",\"choicea\":\"dg<br>\",\"qtype\":\"multiple\",\"title\":\"dfgdfg<br>\",\"choiceb\":\"dg<br>\",\"updated_at\":\"2011-10-10T14:41:52Z\",\"choicec\":\"dfg<br>\",\"choiced\":\"dg<br>\",\"id\":50,\"hint\":\"dg<br>\",\"obsolete\":1,\"correctans\":\"C\"}}

]]"


First there should be just one bracket in your json format you have to change your server code like that. And then u can use this function to get your values from json

$.each(data,function(key,value){
    alert(key+" - "+value.question.title + " - "+value.question.created_at+" ...")
})
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜