Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

May be such problem is not new, but I didn't find anything similar. I have such jQuery code:

$.ajax({    url : ('/people/'+id),    type : 'DELETE',    dataType : 'json',    success : function(e) {     table.getItems(currentPage);   } }); 

My Rails controller looks like this:

def destroy     @person = Person.find(params[:id])     @person.destroy      respond_to do |format|       format.html { redirect_to(people_url) }       format.json  { render :json => @person, :status => :ok }     end end 

This is working.

But when I use the following (as generated by standard), the success callback doesn't get called:

def destroy     @person = Person.find(params[:id])     @person.destroy      respond_to do |format|       format.html { redirect_to(people_url) }       format.json  { head :ok }     end end 

Tested under rails 3.0.3, jQuery 1.4.2 and Firefox 3.6.13.
Firebug says, that query is fired and returns 200 OK in both cases, item is deleted in both cases too. But in second case the callback is not called.

Is there a significant difference in REST, and is there a way to utilize jQuery by using the scaffolded controller?

like image 734
sandrew Avatar asked Jan 25 '11 08:01

sandrew


People also ask

Why is ajax success not working?

ajax post method. The reason was my response was not in the JSON format so there was no need for the dataType: 'json' line in the submit method. In my case, the returned response was in text format that's why it was not going to success event. Solution: Remove dataType: 'json' line.

How can I send success to ajax?

you would use this: $. ajax({ url: "/post/url. php", type: "POST", data: parameters, success: successFunction, error: errorFunction });

What is success in ajax call?

What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.

What is success and error in ajax?

success and Error : A success callback that gets invoked upon successful completion of an Ajax request. A failure callback that gets invoked in case there is any error while making the request.


1 Answers

I have run across this a few times and the answer is deceptively simple.

You are using dataType : 'json' in your $.ajax call, so jQuery is expecting a JSON response. With head :ok Rails returns a response containing a single space (http://github.com/rails/rails/issues/1742), which is not accepted by jQuery as valid JSON.

So if you really expect to get either an error or a bare 200 OK header, just set dataType : 'html' in your request and it should work (if you don't set dataType, jQuery will try to guess at what the type is based on response headers, etc., and could still guess json, in which case you'd still have this problem). If you actually expect to get JSON back, instead of using head :ok render something valid with JSON (see comments) or just use head :no_content as suggested by @Waseem

like image 97
mtjhax Avatar answered Sep 20 '22 18:09

mtjhax