jqueryajaxerror-handlinghttp-status-code-204

jQuery ajax handling of 204 No Content


I am writing an jQuery ajax call to a RESTful server, for which the response to a valid PUT request is an HTTP 204 No Content. The response has a Content Type header of application/text but there is no content body. Because of this, it appears that the ajax call threats this as an error, and so the error function is called instead of done.

Is there a way to override this behaviour, and execute done for a 204 response regardless of the content body?


Solution

  • I finally managed to fix the problem by adding a filter to the ajax request settings

    dataFilter: function(data,type) {
        return data || "{}"
    }
    

    This ensures that JSON.parse never receives empty content when it is called within ajax on the response content.