Rails控制器返回200 OK时不会触发jquery ajax成功回调 [英] jquery ajax success callback not triggered when rails controller returns 200 OK
问题描述
我正在执行标准的ajax请求,并且有一个执行以下操作的Rails控制器操作:
I am doing a standard ajax request and I have a rails controller action that does:
def up_vote
resource.votes.increment!
render :nothing => true
end
这不会触发我的jquery ajax成功回调---但是它会触发statusCode:200回调,但是我想使用成功代替它.轨道渲染文档说使用"head"而不是render:nothing => true,所以我尝试做head:ok,但是结果相同.
This does not trigger my jquery ajax success callback--- It does however trigger the statusCode: 200 callback, but I want to use success instead of that. The rail rendering docs say to use "head" instead of render :nothing => true, so I tried doing head :ok, but that results in the same outcome.
如何使此方法触发成功回调?
How can I make this method trigger the success callback?
推荐答案
在Rails 3.2中使用head :ok
样式响应时,我遇到了同样的问题.我通过在$.ajax
调用中将dataType: "json"
更改为dataType: "text"
来解决此问题.
I've just run into the same problem while using the head :ok
style response with Rails 3.2. I fixed it by changing dataType: "json"
to dataType: "text"
within my $.ajax
call.
如果您指定需要JSON响应,则jQuery似乎不会调用成功回调,但是返回的数据不会验证为JSON.
It seems like jQuery does not call the success callback if you specify wanting a JSON response, but the returned data does not validate as JSON.
这个问题给了我我需要弄清楚的提示: jQuery. Ajax成功回调函数未执行
This question gave me the hint I needed to figure it out: jQuery.ajax success callback function not executed
这篇关于Rails控制器返回200 OK时不会触发jquery ajax成功回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!