jquery AJAX 帖子,服务器反馈“不允许使用方法

jecbmhm3  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(95)

我有一个使用dataTables构建的表,数据来自 AJAX 属性,但是,我在ajax上得到一个错误:
第一个月


的数据
我不知道为什么会这样。下面是我的代码:

<script src="https://cdn.datatables.net/1.13.5/css/jquery.dataTables.min.css"></script>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<script src="https://cdn.datatables.net/1.13.5/js/jquery.dataTables.min.js"></script>

<table id="example" class="display" style="width:100%">
  <thead>
    <tr>
      <th>First name</th>
      <th>Last name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th>First name</th>
      <th>Last name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </tfoot>
</table>

<script>
  $(document).ready(function () {
    const getToken = localStorage.getItem("token");
    const accountType = localStorage.getItem("acc_type");

    console.log("GET TOKEN", getToken);

    var table = $("#example").DataTable({
      processing: true,
      serverSide: true,
      ajax: {
        url: "http://192.168.30.03:3000/api/log/list",
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify({
          limit: 13,
          offset: 1,
          search: "",
          id: 0,
          start_id: 0,
          end_id: 0,
          event: "",
          user: "",
          message: "",
          type: "",
          start_time: "",
          end_time: "",
          order_by: "desc",
          order_column: "id"
        }),
        headers: {
          'Authorization': 'Bearer ' + getToken
        },
        success: function (response) {
          console.log("SUCCESS", response);
        },
        error: function (response) {
          console.log("ERROR", response);
        },
        columns: [
          { data: "id", title: "id" },
          { data: "event", title: "event" },
          { data: "user", title: "user" },
          { data: "message", title: "message" },
          { data: "type", title: "type" },
          { data: "created_at", title: "created_at" },
        ],
      },
    });

    table.draw();

  });
  
</script>

字符串

nimxete2

nimxete21#

  • 这里是.js我来解决我的问题
var storedItem = localStorage.getItem("storedItem") || [];

$(document).ready(() => {
    const getToken = localStorage.getItem("token");
    const API_URL = "http://192.168.30.03:3000/api/log/list";
    var rowData;

    console.log("GET TOKEN", getToken);

    var accountType = localStorage.getItem("acc_type");
    var buttons = [
        {
            extend: "csv",
            text: "CSV",
            className: "btn btn-outline-warning btn-lg",
        },
        {
            extend: "excel",
            text: "Excel",
            className: "btn btn-outline-success btn-lg",
        },

    ];

    var buttonRow = $('<div class="row"></div>');
    
    buttons.forEach(function (button) {
        var buttonCol = $('<div class="col"></div>').append(button);
        buttonRow.append(buttonCol);
    });
    
    
    if (accountType === "OP") {
        buttons = buttons.slice(2);
    }

    var table = $("#example").DataTable({
        processing: true,
        lengthChange: false,
        serverSide: true,
        ajax: {
            url: API_URL,
            type: "POST",    
          
            contentType : 'application/json', // 不能用 'application/json; charset=utf-8'
            data: function(d) {
                d.search = d.search.value
                console.log(d)
                return JSON.stringify(d)
            },
            headers: {
                Authorization: "Bearer " + getToken,
            },
            dataSrc: function (response) {
                console.log("DATA", response);
                return response.data;
            }
        },

        columns: [
            { data: "id", title: "id" },
            { data: "message", title: "message" },
            { data: "type", title: "type" },
            { data: "user", title: "created_by" },
            { data: "created_at", title: "created_at" },
        ],
        dom: '<"row"<"col"B><"col text-right"l><"col"f>>rtip',
        buttons: buttons,

        error:  function(response){
            console.log("ERROR",response);
          }
    });

    table.draw();

字符串

相关问题