2016-03-23 104 views
2

我有一個使用JQuery DataTables的Web應用程序。它使用ajax參數來請求JSON數據並將其插入表中。JQuery DataTables - Catch'ajax'響應

但是,在請求的.php文件的頂部,檢查用戶是否已登錄。如果此檢查失敗,它將回應JSON通知。

<?php 
    session_start(); 
    if (!isset($_SESSION['logged']) || $_SESSION['logged'] !== true) { 
     $array = array(utf8_encode('logged')=>utf8_encode('false')); 
     echo json_encode($array); 
     exit; 
    } 
?> 

table = $('#active-issues').DataTable({ 
     "scrollY": pixels, 
     "dom": '<"top"if>rt<"bottom"><"clear">', 
     "paging": false, 
     "responsive":true, 
     "bProcessing": true, 
     "ajax": { 
      "data": function(){ 
       $('#active-issues').DataTable().ajax.url(
        "php/get_issues.php" 
        + "?id=" + id 
        + "&customer_id=" + customerid 
       ); 
      } 
     }, 
     columns: [ 
      { responsivePriority: 1 }, 
      { responsivePriority: 2 }, 
      { responsivePriority: 4 }, 
      { responsivePriority: 3 }, 
      { responsivePriority: 5 }, 
      { responsivePriority: 6 }, 
      { responsivePriority: 7 } 
     ], 
     "columnDefs": [ 
      { "type": "alt-string", targets: 5}, 
      { "type": "alt-string", targets: 6}, 
     ] 
    }); 

table.ajax.reload(null, false); 

是否有可能趕上給JQuery的數據表的反應如何?以便我可以檢查結果是否爲{ logged: "false" }並採取相應措施?

回答

0

花了一段時間,從datatables論壇'allan'的幫助。但問題終於解決了。

通過dataSrc有可能它是在表打印之前操縱AJAX結果,但是,我用它來檢查結果是否包含logged以及它是否等於false並採取相應的行動,如果它:

"ajax": { 
    "data": function(){ 
     $('#active-issues').DataTable().ajax.url(
      "php/get_issues.php?id=" + id + "&customer_id=" + customerid 
     ); 
    }, 
    "dataSrc": function (json) { 
     if(typeof json['logged'] != "undefined") { 
      if (json['logged'] == 'false') { 
       location.replace('php/logout.php'); 
      } 
     } 
     return json.aaData; 
    } 
}, 
0

是的,這可以通過外部執行你的ajax cal完成,也就是執行一個獨立的ajax請求來獲得響應,如果響應包含 {logged:「false」}並且將數據填充到數據表格。