Ajax 帖子,服务器反馈“方法不允许

Ajax post , server feedback "Method Not Allowed

提问人:code_freshgirl 提问时间:7/17/2023 最后编辑:Travis Heetercode_freshgirl 更新时间:7/17/2023 访问量:38

问:

我有一个使用 dataTable 构建的表,并且数据来自 ajax 属性,但是,我在 ajax 上收到错误:

Ajax post , server feedback "Method Not Allowed"}

Method Not Allowed Error

我不确定为什么会这样。这是我的代码:

<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>

javascript jquery ajax post 数据表

评论

1赞 Jaromanda X 7/17/2023
这表明服务器代码不允许方法 - 但您尚未发布服务器代码POST
0赞 code_freshgirl 7/17/2023
我的另一个同事在服务器端工作,我们前端和后端通过 API 进行通信,而不是服务器代码,我可以证明,但是 [pic-use fastapi work ](imgur.com/a/R3ryAxZfrom website use api call server is not working (the core of this problem)via fastAPI use api call server is working)
1赞 Jaromanda X 7/17/2023
we front-end and back-end communicate via API, instead of server code...哼?那么服务器端没有服务器代码吗?您正在运行什么“后端”?你的同事写了什么 API?你的 API 编写同事知道你做错了什么吗?

答:

0赞 code_freshgirl 7/17/2023 #1

- 这是我提出的解决我问题的.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();

评论

0赞 Community 7/19/2023
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。