我已经在前端建立了一个DataTables表,并进行了服务器端处理,因为这是一个大数据集。我已经如下设置了Ajax调用,以将其回发到同一应用程序中的MVC控制器。
Ajax呼叫:
ajax: {
url: '/test',
type: 'POST',
contentType: 'application/json',
dataType: 'json',
data: function (d) {
return JSON.stringify(d);
}
}
控制器:
[Route("[controller]")]
[AllowAnonymous]
public class TestController : Controller
{
[HttpPost]
public IActionResult Post([FromBody]DataTablesRequest request)
{
// Content removed for brevity
return Ok();
}
}
由于某种原因,我总是返回400错误,并且从未执行过控制器操作。我认为这可能与以下事实有关:我也已经实现了.NET Identity,所以也许这是一个授权问题,但是添加[AllowAnonymous]
属性并没有帮助,我还尝试将xhrFields: { withCredentials: true }
添加到没有影响的Ajax调用。
DataTablesRequest
模型看起来像这样:
public class DataTablesRequest
{
public int Draw { get; set; }
public int Start { get; set; }
public int Length { get; set; }
public DataTablesOrder[] Order { get; set; }
public DataTablesColumn[] Columns { get; set; }
public DataTablesSearch Search { get; set; }
}
public class DataTablesOrder
{
public int Column { get; set; }
public string Dir { get; set; }
}
public class DataTablesColumn
{
public string Data { get; set; }
public string Name { get; set; }
public bool Searchable { get; set; }
public bool Orderable { get; set; }
public DataTablesSearch Search { get; set; }
}
public class DataTablesSearch
{
public string Value { get; set; }
public bool Regex { get; set; }
}
...这是在正文中发送的JSON的示例:
{
"draw":1,
"columns":[
{
"data":"col1",
"name":"",
"searchable":true,
"orderable":true,
"search":{
"value":"",
"regex":false
}
},
{
"data":"col2",
"name":"",
"searchable":true,
"orderable":true,
"search":{
"value":"",
"regex":false
}
},
{
"data":"col3",
"name":"",
"searchable":true,
"orderable":true,
"search":{
"value":"",
"regex":false
}
},
{
"data":"col4",
"name":"",
"searchable":true,
"orderable":true,
"search":{
"value":"",
"regex":false
}
}
],
"order":[
{
"column":1,
"dir":"desc"
}
],
"start":0,
"length":25,
"search":{
"value":"",
"regex":false
}
}
更新:
似乎这实际上与SSL有关。我只是在Postman中发送了请求,并检查了显示以下内容的Postman控制台:
答案 0 :(得分:0)
这是因为您的DataTable ajax发布未正确获取路由。因此,为简单起见,请首先从Controller中删除[Route("[controller]")]
,然后编写如下:
'ajax': {
'url': '@Url.Action("Post", "Test")',
'method': 'POST',
'contentType': 'application/json',
'data': function (d) {
return JSON.stringify(d);
}
}
我已经从工作代码中发布了此信息。
如果您需要整个东西,则如下所示:
var dataTable = $("#yourDataTableId").DataTable({
'processing': true,
'serverSide': true,
"order": [[0, 'desc']],
'ajax': {
'url': '@Url.Action("Post", "Test")',
'method': 'POST',
'contentType': 'application/json',
'data': function (d) {
return JSON.stringify(d);
}
},
"lengthMenu": [[5, 10], [5, 10]],
"lengthChange": true,
"searching": true,
"scrollX": true,
fixedColumns: {
leftColumns: 1,
rightColumns: 1
},
'columns': [
{ data: "yourColumnName", name: "YourColumnName" }
//Other columns
]
});
答案 1 :(得分:0)
您应该从 ApiController 而不是 Controller
来自Get Started with ASP.NET Web API
的示例using ProductsApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
namespace ProductsApp.Controllers
{
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
public IEnumerable<Product> GetAllProducts()
{
return products;
}
public IHttpActionResult GetProduct(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
}
}