Datatables ajax调用错误句柄

时间:2016-02-18 08:08:22

标签: jquery ajax datatables

我正在使用带有ajax调用的datatables,如果出现错误,我想添加错误句柄并重定向到500页。 现在我有这张桌子:

licenseTable = $('#licensesTable').DataTable({
    responsive: true,
    //disable order and search on column
    columnDefs: [
         {
             targets: [4,5],
             //set priority to column, so when resize the browser window this botton stay on the screen because have max priority
             responsivePriority: 1,
             orderable: false,
             searchable: false,
         }
     ],
     //fix problem with responsive table
     "autoWidth": false,
         "ajax":{ 
             "url":"table",
             //Check if there was an error on the query like all other ajax request
             "dataSrc": function ( json ) {
                 if (json.success){
                     return json.result.data;
                 }else{
                     notifyMessage(json.result, 'error');   
                     return "";
                 }                          
             },
     },
     "columns": [
         { "data": "user" },
         { "data": "startDate",
             "render": function (data) {
                 return (moment(data).format("DD/MM/YYYY"));                     
             }
         },
         { "data": "endDate",
             "render": function (data) {
                 return (moment(data).format("DD/MM/YYYY"));                     
             }
         },
         { "data": "counter" },
         { data:null, render: function ( data, type, row ) {
             return '<button type="button" class="btn btn-primary" id="updadteLicense" data-toggle="modal"'
             +'data-target="#updateLicenseModal">Update</button>'

         }
         },
         { data:null, render: function ( data, type, row ) {
             return '<button type="button" class="btn btn-danger" id="deleteLicense" data-toggle="modal"'
             +'data-target="#deleteLicenseModal">Delete</button>'                                   
         }
         }
     ],

});

到目前为止一切顺利,但如果我添加"error": window.location.href = "/ATS/500"它就不再适用了。 我阅读了一些指南,也许我必须使用fnServerData,因为它允许错误处理。 但是如何将我的代码转换为fnServerData?谢谢 我没试这么成功:

licenseTable = $('#licensesTable').DataTable({
    responsive: true,
    //disable order and search on column
    columnDefs: [
         {
             targets: [4,5],
             //set priority to column, so when resize the browser window this botton stay on the screen because have max priority
             responsivePriority: 1,
             orderable: false,
             searchable: false,
         }
     ],
     //fix problem with responsive table
     "autoWidth": false,
     "bProcessing": true,
     "bServerSide": true,
     "sAjaxSource": "table",
     "fnServerData": function ( sSource, aoData, fnCallback ) {
         $.ajax({ 
             "url": sSource,
         });
         $.getJSON( sSource, aoData, function (json) { 
             if (json.success){
                 json=json.result.data;
             }else{
                 notifyMessage(json.result, 'error');   
                 json="";
             }
             fnCallback(json)
         } );
     },

它显示了我的行,但分页,排序和搜索不起作用。没有服务器端,我可以用某种方式使用错误处理吗?

更新:

"url": "table",
"dataSrc": function ( json ) {
    if (json.success){
        return json.result.data;
    }else{
        notifyMessage(json.result, 'error');   
        return "";
    }                           
},  
"error": function (xhr, error, thrown) {
    window.location.href = "/ATS/500";
}

它有效,但什么时候必须进入错误行?如果我使用了错误的url,那么无论如何都会进入dataSrc。

0 个答案:

没有答案