DataTables部署后出现无效JSON响应错误求助(附代码)
问题:DataTables部署后出现无效JSON响应错误
本地运行正常,但部署到Web服务器后,DataTables提示:
DataTables warning: table id=DT_load - Invalid JSON response.
此前遇到404错误,改用命名处理方法(Named Handler methods)后请求状态码变为200,但出现上述无效JSON错误。查阅相关文档后仍无法解决。
systemList.js
$(document).ready(function () { $('#DT_load').DataTable({ ajax: { url: "?handler=SystemsAll", type: "GET", dataSrc: '', dataType: "json", }, columnDefs: [{ visible: false, searchable: false }], columns: [ { "data": "name", "width": "10%"}, { "data": "website", "width": "10%" }, { "data": "wiki", "width": "10%" }, { "data": "lastDeployed", "width": "10%" }, { "data": "sql", "width": "10%" }, { "data": "web", "width": "10%" }, { data: "id", success: function (data) { console.log(data); return `<div class="text-center"> <a href="/AppPages/Update?id=${data}" class='btn btn-success text-white' style='cursor:pointer; width:70px;'> Update </a> <a class='btn btn-danger text-white' style='cursor:pointer; width:70px;' onclick=Delete('/api/system?id='+${data})> Delete </a> </div>`; }, width: "40%" } ], language: { emptyTable: "no data found" }, width: "100%" }); });
HomeController.cs
using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using ServiceCatalogue.Model; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Linq.Dynamic.Core; namespace ServiceCatalogue.Controllers { [Produces("application/json")] public class HomeController : Controller { private readonly AppDbContext _db; public HomeController(AppDbContext db) { _db = db; } public async Task<IActionResult> OnGetSystemsAll() { //var allData = await _db.SystemAsset.ToListAsync(); // return new JsonResult(allData); return Json(new { data = await _db.SystemAsset.ToListAsync() }); } public async Task<IActionResult> OnPostDelete(int id) { var systemFromDb = await _db.SystemAsset.FirstOrDefaultAsync(u => u.Id == id); if (systemFromDb == null) { return Json(new { success = false, message = "Error while Deleting" }); } _db.SystemAsset.Remove(systemFromDb); await _db.SaveChangesAsync(); return Json(new { success = true, message = "Delete successful" }); } } }
Index.cshtml(注:原内容粘贴重复为HomeController代码,推测此处应为页面视图代码,若有遗漏请补充)
<!-- 此处应为Index视图的HTML代码,原内容粘贴错误,暂留空 -->
Startup.cs
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using ServiceCatalogue.Model; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Serialization; using System.Text.Json; namespace ServiceCatalogue { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // 此方法由运行时调用,用于向容器添加服务 public void ConfigureServices(IServiceCollection services) { services.Configure<IISServerOptions>(options => { options.AutomaticAuthentication = false; }); services.AddDbContext<AppDbContext>(option => option.UseSqlServer(Configuration.GetConnectionString("CatologueDb"))); services.AddControllersWithViews(); services.AddMvc() .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()); services.AddRazorPages(); } // 此方法由运行时调用,用于配置HTTP请求管道 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // 默认HSTS值为30天,生产环境可根据需要修改 app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapRazorPages(); }); } } }
内容的提问来源于stack exchange,提问作者RumenDimov_1
相关产品推荐
相关产品推荐

