tags:

views:

53

answers:

2

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?

+2  A: 
jQuery.ajax({
    ...
    error: function(xhr, errorText) {
        if(xhr.status==204) successCallback(null, errorText, xhr);
        ...
    },
    ...
});

ugly...but might help

sje397
Thanks sje397, that makes sense. You think it's the only way?
Bennidhamma
+1  A: 

204 should be treated as success. What version of jQuery are you using? I did a couple of tests and all 200 range status codes went to the success handler. The source for jQuery 1.4.2 confirms this:

// Determines if an XMLHttpRequest was successful or not
httpSuccess: function( xhr ) {
    try {
        // IE error sometimes returns 1223 when 
        // it should be 204 so treat it as success, see #1450
        return !xhr.status && location.protocol === "file:" ||
            // Opera returns 0 when status is 304
            ( xhr.status >= 200 && xhr.status < 300 ) ||
            xhr.status === 304 || xhr.status === 1223 || xhr.status === 0;
    } catch(e) {}

    return false;
},
Anurag
Okay, now I'm confused. I'm trying this again, and it appears to be working. It looks like I must have been doing something else incorrectly.Thanks!
Bennidhamma