jquery doesn't call success method on $.ajax for rails standard REST DELETE answer -
may such problem not new, didn't find similar. have such jquery code:
$.ajax({ url : ('/people/'+id), type : 'delete', datatype : 'json', success : function(e) { table.getitems(currentpage); } });
my rails controller looks this:
def destroy @person = person.find(params[:id]) @person.destroy respond_to |format| format.html { redirect_to(people_url) } format.json { render :json => @person, :status => :ok } end end
this working.
but when use following (as generated standard), success
callback doesn't called:
def destroy @person = person.find(params[:id]) @person.destroy respond_to |format| format.html { redirect_to(people_url) } format.json { head :ok } end end
tested under rails 3.0.3
, jquery 1.4.2
, firefox 3.6.13
.
firebug says, query fired , returns 200 ok in both cases, item deleted in both cases too. in second case callback not called.
is there significant difference in rest, , there way utilize jquery using scaffolded controller?
i have run across few times , answer deceptively simple.
you using datatype : 'json'
in $.ajax call, jquery expecting json response. head :ok
rails returns response containing single space (http://github.com/rails/rails/issues/1742), not accepted jquery valid json.
so if expect either error or bare 200 ok header, set datatype : 'html'
in request , should work (if don't set datatype, jquery try guess @ type based on response headers, etc., , still guess json, in case you'd still have problem). if expect json back, instead of using head :ok
render valid json (see comments) or use head :no_content
suggested @waseem
Comments
Post a Comment