Rails的,阿贾克斯,POST功能,jQuery的 [英] Rails , Ajax , Post Function , Jquery
问题描述
我的工作on Rails和有问题的ajax post数据呼叫。这是工作和数据被输入数据库但成功功能无法正常工作,在成功的功能警报功能不点火。
I am working on Rails and having problem with ajax post data call. It is working and data is entered in db but the SUCCESS function is not working, as alert function in success function is not firing.
另外我想补充意见,以我的职务而无需刷新整个页面。
Further I want to add comment to my post without refreshing whole page.
AJAX code是这样的:
AJAX code looks like :
<script type="text/javascript">
$(document).ready(function () {
$('form').submit(function () {
$.ajax({
url: '/comments/create',
type: "POST",
dataType: 'json',
processData: false,
success: function (msg) {
alert(msg);
},
error: function (xhr, status) {
alert(xhr.error);
}
});
});
});
</script>
和控制器code是:
def create
puts 'params', params.inspect
@post = Post.find(params[:post_id])
@comment = @post.comments.new(params[:comment])
respond_to do |format|
if @comment.save
format.html { redirect_to(@post, :notice => 'Thanks for comment.') }
format.json { render json => @post, :msg=>"comment oK" }
else
format.html { render :action => "new" }
format.json { render :xml => @comment.errors, :msg=>"comment oooK", :status => :unprocessable_entity }
end
end
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 alert in success function is not working and alert in error function is firing.
推荐答案
尝试:
$('form').submit(function()
{
var myForm = $('form').serialize();
$.ajax
({
url:'/comments/create',
type:"POST",
dataType:'json',
data: myForm,
processData:false,
success: function (msg)
{
alert(msg);
},
error: function (xhr, status)
{
alert(xhr.error);
}
});
});
如果这不起作用,请写你跨线发送您的AJAX请求是什么。使用Chrome控制台或Firebug检查并粘贴发表结果在这里。
If this doesn't work, please write what you're sending across the wire in your AJAX request. Use Chrome Console or Firebug to check and paste the POST results in here.
这篇关于Rails的,阿贾克斯,POST功能,jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!