如何使用jQuery Datatables fnServerData来处理HTTP 403

时间:2012-07-19 13:38:38

标签: jquery datatables

我正在为ajax调用生成会话超时的http 403。使用jquery Datatables时,我可以使用fnServeData拦截返回的调用,如此

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

通常只是将结果转发到数据表上 - 但是当我返回403时,我只是在我的Firebug控制台中得到http错误 - 我如何/在哪里检查403以便我可以显示对话框?

2 个答案:

答案 0 :(得分:7)

经过一番挖掘 - 一个解决方案是添加一个错误处理程序,如下所示:

"fnServerData": function ( sSource, aoData, fnCallback ) {
    $.getJSON( sSource, aoData, function (json) { 
            fnCallback(json);
        });
    }.error(function(jqXHR, statusText, errorThrown) { 
    console.log(jqXHR.status);//403 etc.
});

答案 1 :(得分:1)

接近空白的回答,但我的看法如下:

"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);

    }

createLog.php文件如下所示:

    <?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);

    }
    ?>