Ajax handling status code 400 for invalid new record

Ajax handling status code 400 for invalid new record

pmj7pmj7 Posts: 18Questions: 5Answers: 0

I'm setting up a REST interface and following the example as shown on the site:
http://editor.datatables.net/examples/advanced/REST.html
To me, the right response for field validation errors is a 400 error:
400 Bad Request
"The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing)."
But when Datatables sees that status code, it shows 'A system error has occurred' at the bottom of the Editor dialog. I'm including the fieldErrors within my JSON, etc, but the sticking point is the status code. If I use a 200 series status code, which DataTables will accept, it won't be a proper REST API.

Suggestions?

Peter

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    Hi Peter,

    Thanks for posting this - I absolutely see your point about this and I will add better handling for this in Editor. The reasoning I have for using 2xx only as success is that the server will have correctly handled the data, even if it is in error - hence the "ok" response, but I agree this isn't really how REST works.

    I will have a think about how best to fold this into Editor and post back here, but if you want an immediate solution you can use ajax as a function thus:

    ajax: function ( method, url, data, success, error ) {
      $.ajax( {
        url: '/api/...',
        type: 'POST',
        dataType: 'json',
        success: success,
        error: error
        statusCode: {
          400: success
        }
    }
    

    I may have Editor simply run its success handler for all status code valid which contain valid JSON (I think it is unlikely that e "real" error will return valid JSON - and even if it does, the error option could still be used). The benefit of that is that it will automatically just work. I'll keep you posted.

    Regards,
    Allan

  • pmj7pmj7 Posts: 18Questions: 5Answers: 0

    Hi

    Sounds good in theory, but I couldn't get it to work. I tried various combinations for the statusCode parameter, no joy. Ditto all combinations of 'returns' from an anonymous function for statusCode, no help either. The only thing that works is catching the status code within the error function, converting the json, and calling the success function yourself. Any more elegant methods would be appreciated.

    Thanks

    Peter

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Answer ✓

    It looks like jQuery will execute the statusCode functions as well as success and error (as required) rather than allowing the statusCode to override the callbacks if that status code is returned as I had expected it to work.

    Could you try simply commenting out the error: error line - that should allow it to work.

    Regards,
    Allan

  • pmj7pmj7 Posts: 18Questions: 5Answers: 0

    Turns out that if there is an error I have to massage the response from my REST API to best work with DataTables Editor, So I'm leaving the behaviour as it is, success pretty much calls up the DataTables callback, and error does some work.

    Thanks
    Peter

This discussion has been closed.