简体   繁体   中英

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.

Javascript (actually 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.

Dam it.

The problem was that the JSON was malformed.

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)

So if I change this to

format.json {render json: @image}

I get valid json and the done callback fires...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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