当rails控制器返回200 OK时,不会触发jquery ajax成功回调

我正在做一个标准的ajax请求,我有一个rails控制器动作:

def up_vote resource.votes.increment! render :nothing => true end 

这不会触发我的jquery ajax成功回调—它会触发statusCode:200回调,但我想使用成功而不是那样。 铁路渲染文档说使用“head”而不是render:nothing => true,所以我尝试做头:好的,但这导致了相同的结果。

如何使此方法触发成功回调?

我在使用head :ok时遇到了同样的问题head :ok使用Rails 3.2的head :ok样式响应。 我通过在$.ajax调用dataType: "json"更改为dataType: "text"来修复它。

如果您指定想要JSON响应,jQuery似乎不会调用成功回调,但返回的数据不会validation为JSON。

这个问题给了我提示我需要弄清楚: jQuery.ajax成功回调函数没有执行

我通过渲染json响应来修复此错误。 render :json => { :success => "success", :status_code => "200" }

老问题,但它在搜索引擎中很高,所以给它一个答案:)

你可以这样做:

 def up_vote resource.votes.increment! render :nothing => true respond_to do |format| format.json { render json: {} , status: 200 } end end