Ruby on rails 如何在RoR中访问Jquery的Ajax调用?
伙计们,我真的是这个RoR的新手,在这一刻我遇到了复杂的情况,如何在RoR中使用Jquery的ajax调用 我有一个控制器叫做这样的项目Ruby on rails 如何在RoR中访问Jquery的Ajax调用?,ruby-on-rails,ajax,jquery,Ruby On Rails,Ajax,Jquery,伙计们,我真的是这个RoR的新手,在这一刻我遇到了复杂的情况,如何在RoR中使用Jquery的ajax调用 我有一个控制器叫做这样的项目 class ProjectsController < ApplicationController def stagemilestone @milestones=Milestone.find_by_sql("SELECT name, created_at FROM milestones WHERE stage=1") end e
class ProjectsController < ApplicationController
def stagemilestone
@milestones=Milestone.find_by_sql("SELECT name, created_at FROM milestones WHERE stage=1")
end
end
$.ajax({
url: "/projects/stagemilestone",
success: function(){
//here i need the returned data from controller
// means output of @milestones
}
});
$.ajax({
type : 'get',
url : "/projects/stagemilestone",
data : "stageid=" + $(this).attr('name') + "&id=" + $.cookie('projectid'),
dataType : 'json',
async : false,
context : document.body,
success : function(response) {
}
});
因此,请帮助我,如何做到这一点?在成功回调中添加一个参数;它将包含响应值
success: function(response)
{
// response will be the HTTP / JSON / text returned from your controller
}
只需向回调函数添加一个参数,如:
$.ajax({
url: "/projects/stagemilestone",
success: function(output){
//Do something with 'output'
}
});
我想你想要的是用你来回应 像这样
class ProjectsController < ApplicationController
def stagemilestone
@milestones=Milestone.find_by_sql("SELECT name, created_at FROM milestones WHERE stage=1")
end
end
$.ajax({
url: "/projects/stagemilestone",
success: function(){
//here i need the returned data from controller
// means output of @milestones
}
});
class ProjectsController < ApplicationController
def stagemilestone
@milestones=Milestone.find_by_sql("SELECT name, created_at FROM milestones WHERE stage=1")
respond_to do |format|
format.js {render :json => @milestones}
end
end
end
$.ajax({
type : 'get',
url : "/projects/stagemilestone",
data : "stageid=" + $(this).attr('name') + "&id=" + $.cookie('projectid'),
dataType : 'json',
async : false,
context : document.body,
success : function(response) {
}
});
这里有更多关于回复的信息
更新:您可能需要仔细检查ajax请求的accept标头。您可以在firebug中查看该请求,您可能需要使用format.json。有关MIME类型的完整列表,请参阅此处,并确保它们匹配:
伙计们,我终于找到了如下解决方案,效果很好 控制器 我的char.js看起来像这样
class ProjectsController < ApplicationController
def stagemilestone
@milestones=Milestone.find_by_sql("SELECT name, created_at FROM milestones WHERE stage=1")
end
end
$.ajax({
url: "/projects/stagemilestone",
success: function(){
//here i need the returned data from controller
// means output of @milestones
}
});
$.ajax({
type : 'get',
url : "/projects/stagemilestone",
data : "stageid=" + $(this).attr('name') + "&id=" + $.cookie('projectid'),
dataType : 'json',
async : false,
context : document.body,
success : function(response) {
}
});
你这样做了,但仍然不起作用,告诉我2件事1]我需要添加行动回报吗?假设class ProjectsController