DataTables警告,无效的JSON响应



我无法从数据表中生成任何数据。它在localhost上运行得很好,但现在该网站托管在IIS上,我得到了一个无效的JSON响应。

我已经启动了开发工具,并在网络选项卡中获得了以下

Request URL: http://odgh-webapp-uat/masks?_=1604435258024
Request Method: GET
Status Code: 200 OK
Remote Address: 10.146.16.194:80
Referrer Policy: strict-origin-when-cross-origin
Content-Type: text/html; charset=utf-8
Date: Tue, 03 Nov 2020 20:27:36 GMT
Server: Microsoft-IIS/10.0
Transfer-Encoding: chunked
X-Powered-By: ASP.NET
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en;q=0.9,en-US;q=0.8
Connection: keep-alive
Cookie: .AspNetCore.Antiforgery.6A-HjuGlSgs=CfDJ8FCglAuCLlJGqq2XVQMUUAUSieaecoWLJn7jobDBfMTOjwq6Zt8QUnrQJ7nS_GGmILzcdiyPJZG57YAQjb9R20dqsl9bbTc_mcmCwWDitum3mzfCW6Hz40GleUM8pqCY1ddr0isyCFOTohiI09hlJ4o
Host: odgh-webapp-uat
Referer: http://odgh-webapp-uat/masks/MaskList
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36 Edg/86.0.622.61
X-Requested-With: XMLHttpRequest

看看我的Jquery在下面

var dataTable;
$(document).ready(function () {
loadDataTable();
});
function loadDataTable() {
dataTable = $('#DT-load').DataTable({
"ajax": {
"url": "/masks",
"type": "GET",
"datatype": "json",
"dataSrc": "data",
},
"columns": [
{ "data": "name", "width": "20%" },
{ "data": "author", "width": "20%" },
{ "data": "isbn", "width": "20%" },
{
"data": "id",
"render": function (data) {
return `<div class="text-center">
<a href="/MaskList/Edit?id=${data}" class='btn btn-success text-white' style='cursor:pointer; width:70px;'>
Edit
</a>
&nbsp;
<a  class='btn btn-danger text-white mx-auto' style='cursor:pointer; width:70px;'
onclick=Delete('/masks?id='+${data})>
Delete
</a>
</div>`;
}, "width": "40"
}
],
"language": {
"emptyTable": "No Data found"
},
"width": "100%"
});
}

//Sweet Alert Function
function Delete(url) {
swal({
title: "Are you sure?",
text: "Once Delete you will not be able to recover",
icon: "warning",
buttons: true,
dangerMode: true
}).then((willDelete) => {
if (willDelete) {
$.ajax({
type: "DELETE",
url: url,
success: function (data) {
if (data.success) {
toastr.success(data.message);
dataTable.ajax.reload();
}
else {
toastr.error(message);
}
}
});
}
})
}

我的控制器看起来是正确的,在localhost 期间运行良好

namespace MaskFit.Controllers
{
[Route("/mask")]
[ApiController]
public class MaskController : Controller
{
private readonly ApplicationDbContext _db;
public MaskController(ApplicationDbContext db)
{
_db = db;
}
[HttpGet]
public async Task<IActionResult> GetAll()
{
return Json(new { data = await _db.Mask.ToListAsync() });
}

我是不是遗漏了一些显而易见的东西?感谢的任何帮助

您应该确保从服务器返回的数据包含如下属性数据:

{
"data": [
{...}
{...}
]
}

否则,应指定";dataSrc":"quot;

最新更新