Rails: "head: ok" is interpreted as "ajax: error"

Possible duplicate:
jquery doesn't call success method on $ .ajax for rails standard REST DELETE answer

I respond to the remote link (data-remote = "true" data-type = "json") and outputs

format.json { head :ok } 

in my Rails controller (3.2.6), which creates this header:

 Status Code:200 OK 



 ... Connection:keep-alive Content-Length:1 Content-Type:application/json; charset=utf-8 Server:thin 1.4.1 codename Chromeo Set-Cookie: ... path=/; HttpOnly X-UA-Compatible:IE=Edge ... 

In my JavaScript file , ajax: complete is run and 200 (data.status) is output.

  $( '#myElement' ).on( 'ajax:complete', function( e, data ) { console.log( data.status ); }); 

The data is as follows:

  ... readyState: 4 responseText: " " setRequestHeader: function ( name, value ) {... state: function () {... status: 200 statusCode: function ( map ) {... statusText: "OK" ... 

Looks good to me ...

Problem

Instead of ajax: success jQuery (jquery-ujs) executes ajax: error , and I have no idea why, because the error is not indicated.

I thought a lot about the discussions, but in this way it always seemed like a solution, not a problem. Thanks for any help!

+26
jquery ruby-on-rails-3
Sep 13 '12 at 13:23
source share
1 answer

Answered here .

jQuery is expecting a JSON response. "head: ok" has one space for the response body, so jQuery cannot parse the JSON response, so it still considers status code 200 to be an error.

You can configure Rails to head :no_content , which has an empty body or render :json=>true

This was discussed in Rails here .

The reason for the single space in head :ok in Rails is the workaround in the old Safari error.

+41
Oct 05 '12 at 17:34
source share



All Articles