简体   繁体   English

rails / jquery / ajax:完成的回调未执行

[英]rails/jquery/ajax: done callback is not executing

I had working code where an AJAX call which didn't require a return value was working correctly. 我的工作代码中不需要返回值的AJAX调用正常工作。

Javascript (actually coffeescript): Javascript(实际上是coffeescript):

$.ajax({
      url: "/images/" + image_id + "/" + action,
      type: "GET",
      dataType: 'json'
    }).done( (response) ->
      alert("hello")
      )

At server side I had this: 在服务器端,我有这个:

respond_to do |format|
  format.js {render nothing: true}
end

This was working fine 这很好

But now I need to return a value, so I changed server side to this: 但是现在我需要返回一个值,因此我将服务器端更改为:

respond_to do |format|
      format.json {render :json => @image.quality}
end

This also executes fine, no errors server-side nor client-side - but the "done" callback never executes. 这样也可以很好地执行,服务器端和客户端都不会出错-但是“完成”回调永远不会执行。

What am I missing? 我想念什么? I searched lots of docs and I am confused about what I've found. 我搜索了许多文档,但对发现的内容感到困惑。 The official docs http://www.railscook.com/recipes/rails-view-controller-ajax-response-example/ say to render js with a template but I don't need to render javascript directly, I just need a value at client-side. 官方文档http://www.railscook.com/recipes/rails-view-controller-ajax-response-example/说使用模板渲染js,但是我不需要直接渲染javascript,我只需要一个值在客户端。

Dam it. 该死的

The problem was that the JSON was malformed. 问题是JSON格式错误。

For some reason 由于某些原因

format.json {render :json => @image.quality}

doesn't produce valid json, and actually, not the done callback, but the error callback would fire...(very bad practice from my side to not have included an error callback in the first place) 不会产生有效的json,实际上不是完成的回调,但是会触发错误回调...(从我这一角度来看,很糟糕的做法是首先没有包含错误回调)

So if I change this to 因此,如果我将其更改为

format.json {render json: @image}

I get valid json and the done callback fires... 我得到有效的json并且完成的回调触发了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM