Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jQuery Datatables fnServerData to handle HTTP 403

I'm generating a http 403 on session timeout for ajax calls. When using jquery Datatables I can use fnServeData to intercept the returning call like this

"fnServerData": function ( sSource, aoData, fnCallback ) {
    $.getJSON( sSource, aoData, function (json) { 
            fnCallback(json);
        });
    }

Which normally just forwards the result onto the datatable - but when I return a 403 i just get the http error in my Firebug console - how / where can i check for the 403 so i can display a dialog?

like image 417
blank Avatar asked Jul 19 '12 13:07

blank


2 Answers

After a bit more digging - one solution is to add an error handler like this:

"fnServerData": function ( sSource, aoData, fnCallback ) {
    $.getJSON( sSource, aoData, function (json) { 
            fnCallback(json);
        });
    }.error(function(jqXHR, statusText, errorThrown) { 
    console.log(jqXHR.status);//403 etc.
});
like image 127
blank Avatar answered Nov 15 '22 05:11

blank


Close to blank's answer, but mine looks like this:

"sServerMethod"   : "POST",
"fnServerData": function (sSource, aoData, fnCallback) {
               $.getJSON(sSource, aoData, function (json) {
                    fnCallback(json);
                }).done(function(){

                }).fail(function handleError(jqXHR, statusText, errorThrown) {
                    alert("The page cannot load.  Refresh your browser or if this problem persists, contact support.");
                    sendError(jqXHR,statusText,errorThrown);

            });
        }

function sendError(jqXHR, statusText, errorThrown){
        jqXHR = JSON.stringify(jqXHR) || '';
        statusText = statusText || '';
        errorThrown = errorThrown || '';

        var data = new FormData();
        data.append('jqXHR', jqXHR);
        data.append('statusText', statusText);
        data.append('errorThrown', errorThrown);
        var file = window.location.href;
        data.append('file', file); 

        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'createLog.php', true);
        xhr.onload = function () {
            // test if you want to know when errors are actually thrown.

        };
        xhr.send(data);

    }

The createLog.php file looks like this:

    <?php

    if(!function_exists('convertAlphaNumWithSpace')){
        function convertAlphaNumWithSpace($value) {
          if( !isset($value) || is_null($value) ){
            return false;
          }

          $value = trim($value);
          if (ctype_alnum($value)) {
            return $value;
          }
          $value = preg_replace("/[^'\"a-zA-Z0-9_. ]/", '', $value);
          return $value;


        }
    }
    if(!function_exists('filledAndSet')){
        function filledAndSet($postVar){

            if(is_null($postVar)){
                return false;
            } 

            if(!is_array($postVar)){
                $postVar = trim($postVar);

                if(is_bool($postVar)){
                   return true;
                }   

                if(is_int($postVar)){
                return true;
                }

                if(isset($postVar) && !empty($postVar)){
                    return true;
                } 
                return false;
            }/* End basic bool, int, string check.  Although false will return as false, unsurprisingly. */

            if(is_array($postVar)){
                foreach($postVar as $key => $value) {
                    $$key = trim($value);
                    if(empty($$key)){
                        return false;
                    }/* We already know that the array is set, or it would never pass isarray */
                }return true; /* If the array is set and not empty, return true. */

                }
        }/* End filled and set. */
    }

    filledAndSet($_POST['jqXHR']) ? $mainLog = convertAlphaNumWithSpace($_POST['jqXHR']) : $mainLog = null;
    filledAndSet($_POST['statusText']) ? $statusText = convertAlphaNumWithSpace($_POST['statusText']) : $statusText = null;
    filledAndSet($_POST['errorThrown']) ? $errorThrown = convertAlphaNumWithSpace($_POST['errorThrown']) : $errorThrown = null;
    filledAndSet($_POST['file']) ? $file = $_POST['file'] : $file = null;
    if(!is_null($mainLog)){
        $thisDate = date("F j, Y, g:i a");  
        $prettyPrint = "$mainLog  \r\n $statusText \r\n $errorThrown \r\n $file \r\n $thisDate \r\n\r\n"; 
        $logFile = __DIR__ . DIRECTORY_SEPARATOR . 'json.log';
        if(!file_exists($logFile)){
            touch($logFile);
            chmod($logFile, 0777);
        }
        file_put_contents($logFile, $prettyPrint, FILE_APPEND);

    }
    ?>
like image 38
Michael Ryan Soileau Avatar answered Nov 15 '22 04:11

Michael Ryan Soileau