使用jQuery DataTables发送之前

时间:2018-09-27 11:36:10

标签: ajax datatables

我该如何在jQuery datatables中使用BeforeSend ajax属性?因为我的服务器端API希望进行身份验证。我使用标头代替了BeforeSend,但是却出现了405错误

$(document).ready(function() {
  var username = localStorage.getItem("Username");
  var password = localStorage.getItem("Password");
  var url = "https://lim.com.tr/app/public/api/v1/policelerim";
  var table = $('#example').DataTable({
    "processing": false,
    "serverSide": false,
    "ajax": {
      url: url,
      type: "POST",
      BeforeSend: {
        'Authorization': "Basic " + btoa(username + ":" + password)
      },
    }
  });
});

修改后的代码

    $(document).ready(function() {
    var username="info@lim.com.tr";//localStorage.getItem("Username");
        var password="102030asd";//localStorage.getItem("Password");
        var url="https://lim.com.tr/app/public/api/v1/policelerim";






 $('#example')
    .on('preXhr.dt', function ( e, settings, data ) {
        data.Authorization = "Basic " + btoa(username + ':' + password);
    } ).dataTable( {
            "processing": true,
            "serverSide": true,
            "ajax":{
                "url": url,
                "type": "POST"

            }

} );

} );

0 个答案:

没有答案