问题描述
我是个Rails的新手,有一个关于从阿贾克斯发布到Rails控制器,以及将数据返回给JavaScript的问题。我是pretty的不熟悉这实际上是如何工作的,但我已经拼凑其他SOF线程工作岗位功能。现在,我张贴从一个简单的按钮,点击数据,以及阿贾克斯code是这样的:
I am a Rails newbie and have a question about posting from ajax to a rails controller, and returning the data to javascript. I'm pretty unfamiliar with how this actually works, but I've pieced together a working post function from other SOF threads. Right now, I'm posting the data from a simple button click, and the ajax code looks like this:
$.ajax({
type : 'POST',
url : "http://localhost:3000/fbusers",
data : { tester : { name : 'boom' } },
success : function(data) {
alert(data);
},
});
下面是code控制器,它的pretty的简单:
Here is the code in the controller, its pretty simple:
def create
@user = User.create( params[:tester] )
end
这code正常工作,把数据上传到创建功能,它创建(职位?)项的分贝。不过,我想回的数据后,报警功能的成功。但现在,该警报只是返回对象[对象]。谁能帮我交的数据传递到报警功能?如果可能的话,解释(或方向的基础教程)将极有帮助。
This code works fine to post the data to the create function, which creates (posts?) the entry to the db. But I would like to return the data in the alert function upon "success." But right now, the alert just returns object[object]. Can anyone help me pass the post data into the alert function? If possible, an explanation (or direction to a basic tutorial) would be extremely helpful as well.
在此先感谢!
推荐答案
我假设你想返回给用户。在这种情况下,我建议:
I assume you want to return the user. In this case i recommend:
def create
@user = User.create( params[:tester] )
respond_to do |format|
format.html
format.json { render :json => @user}
end
end
然后只是使Ajax调用请求的JSON
And then just make the ajax call request the json
这篇关于jQuery的,阿贾克斯,POST功能,滑轨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!