1

I'm getting different results if I render js versus render json .Trying to figure out why this doesn't work quite as expected

My javascript looks like this:

$( function () {
    $('.delete_post').bind('ajax:success', function () {
            $(this).closest('tr').fadeOut();
        }
    )
});

Controller:

Works

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

Works

respond_to do |format|
  format.js { head :ok }
end

Works

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

Doesn't work

respond_to do |format|
  format.json { head :ok }
end

Doesn't work

respond_to do |format|
  format.json { render :nothing => true }
end

When I examine what is happening in the ajax response I see that the cases where it doesn't work I get a "parseError".

I understand that the Content-type getting sent back is different (text/javascript vs applicaiton/json), but I would still expect the last 2 cases to work since they are sending back a 200 to the server.

(Code derived from: http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/)

Hortitude
  • 13,638
  • 16
  • 58
  • 72
  • You may find this helpful: http://stackoverflow.com/questions/5213956/what-means-location-and-head-ok-in-the-respond-to-format-statemen – PhillipKregg Mar 15 '12 at 21:45
  • That link is helpful, but just explains why/how to send a 200 response. I know that I am sending a 200 response in the "Doesn't work" cases, but still getting into a "parseError" within the javascript – Hortitude Mar 16 '12 at 18:14

1 Answers1

0
format.json { head :ok }

and

format.json { render :nothing => true }

don't work because they're not returning valid json. head :ok will simply set the response header to 200 and :nothing => true will render a blank page/document which not valid json.

sjobe
  • 2,817
  • 3
  • 24
  • 32