提问人:code_freshgirl 提问时间:7/17/2023 最后编辑:Travis Heetercode_freshgirl 更新时间:7/17/2023 访问量:38
Ajax 帖子,服务器反馈“方法不允许
Ajax post , server feedback "Method Not Allowed
问:
我有一个使用 dataTable 构建的表,并且数据来自 ajax 属性,但是,我在 ajax 上收到错误:
Ajax post , server feedback "Method Not Allowed"}
我不确定为什么会这样。这是我的代码:
<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>
答:
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();
评论
POST
from website use api call server is not working (the core of this problem)
via fastAPI use api call server is working
)we front-end and back-end communicate via API, instead of server code
...哼?那么服务器端没有服务器代码吗?您正在运行什么“后端”?你的同事写了什么 API?你的 API 编写同事知道你做错了什么吗?