Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

success handler for $.ajax function with a non-standard http status code

Tags:

jquery

ajax

We've written a RESTful server API. For whatever reason, we made the decision that for DELETEs, we would like to return a 204 (No Content) status code, with an empty response. I'm trying to call this from jQuery, passing in a success handler and setting the verb to DELETE:

jQuery.ajax({
    type:'DELETE',
    url: url,
    success: callback,
});

The server returns a 204, but the success handler is never called. Is there a way I can configure jQuery to allow 204s to fire the success handler?

like image 926
Bennidhamma Avatar asked Jun 18 '10 16:06

Bennidhamma


1 Answers

It's technically a problem on your server
Like Paul said, an an empty 204 response with a server content-type of json is treated by jQuery as an error.

You can get around it in jQuery by manually overriding dataType to 'text'.

$.ajax({
    url: url,
    dataType:'text',
    success:(data){
       //I will now fire on 204 status with empty content
       //I have beaten the machine.
    }
});
like image 164
Will Stern Avatar answered Sep 28 '22 09:09

Will Stern