An error occur when http response isn't 200

An error occur when http response isn't 200

NickZhangNickZhang Posts: 11Questions: 7Answers: 0
edited September 2015 in DataTables 1.10

Hi , when i sumbit a create /edit request to the server, as the session timeout or permission denies got 403 response code,

the editor just stuck there and got no response, my question is: is there any way to put a hook to handle such special http response code ?

This question has an accepted answers - jump to answer

Answers

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    That depends on how you are sending updates to the server. sample code please.

  • NickZhangNickZhang Posts: 11Questions: 7Answers: 0
                            editor = new jQuery.fn.dataTable.Editor( {
                         "ajax": function ( method, url, data, sccess, error ) {
                             jQuery.ajax( {
                                    type: 'POST',
                                    contentType: 'application/json',
                                    url:  'serverURL',
                                    data: JSON.stringify(data),
                                    dataType: "json",
                                    success: function (json) {
                                        sccess( json );
                                    },
                                    error: function (xhr, error, thrown) {
                                        error( xhr, error, thrown );
                                    }
                                } );
                            },
    
  • ThomDThomD Posts: 334Questions: 11Answers: 43

    All that http error detail should be in that error function on line 13. You'll need to do some debugging to see if that line is being called correctly.

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓

    Now that I'm at work, I can post a sample. I renamed some of the passed-in parameters because I found the variable names in the examples hard to keep track of.

    dtEditor = new $.fn.dataTable.Editor( {
    [SNIP]
        //this defines how the editor submits data
        ajax: function( method, url, submitData, dtSuccessCallback, dtFailureCallback) {
            $.ajax({
                type: "POST",
                url: myAjaxURL,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify({ ...my data here ...}),
                cache: false,
                processData: true,
                success: function (data, status, xhr) {
                                  ... do something with the data that comes back... 
                },
                error: function (xhr, jqAjaxerror, thrown) {
                    if ( xhr.status == 412 ) {                      
                        alert("Your change could not be saved because the record has been updated by someone else. Please refresh the screen and enter your change again.");
                    } else {
                        alert("Your change could not be saved. Please contact Supper for support with this error information " + xhr.status + "  "+ thrown +". Please note the time and which item you were updating.");
                    }
    //This tells dataTables editor that the update failed                       
                    dtFailureCallback( jqAjaxerror );
                }
            });
        } 
    } );
    
This discussion has been closed.