Javascript 为什么在从.net core controller调用ajax datatable api时总是出现400错误?

Javascript 为什么在从.net core controller调用ajax datatable api时总是出现400错误?,javascript,ajax,asp.net-core,.net-core,Javascript,Ajax,Asp.net Core,.net Core,cmjobprogress.js var JobdataTable; $(document).ready(function () { loadJobDataTable(); }); function loadJobDataTable() { JobdataTable = $('#JobtblData').DataTable({ "ajax": { "url": "/Admin/C

cmjobprogress.js

    var JobdataTable;

$(document).ready(function () {
    loadJobDataTable();
});

function loadJobDataTable() {
    JobdataTable = $('#JobtblData').DataTable({
        "ajax": {
            "url": "/Admin/CMJobProgress/GetAll"
        },
        "columns": [
            { "data": "job_number" },
            { "data": "assign_from" },
            { "data": "assign)to" },
            { "data": "date_assign" },
            { "data": "job_action" },
            { "data": "remarks" },
            { "data": "type" },
            { "data": "division" }
        ]

    })
}
CMJobProgressController.cs

#region API Calls
        [ValidateAntiForgeryToken]
       [HttpGet]
       public IActionResult GetAll()
        {
            var allObj = _unitOfWork.cmJobProg.GetALL(j => j.JobNumber=="19950232"); 

            return Json(new { data = allObj });
        }

#endregion
Index.cshtml

@{

    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0 text-dark">Job Action</h1>
            </div><!-- /.col -->
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="cmd.html">Home</a></li>
                </ol>
            </div><!-- /.col -->
        </div><!-- /.row -->
        <div class="row mb-2">
            <div class="col-6 text-right">
            </div>
            <div class="col-6 text-right">
                <a class="btn btn-primary"><i class="fas fa-plus"></i> &nbsp; Create New Job</a>
            </div>
        </div>

    </div><!-- /.container-fluid -->
</div>

<section class="content">
    <div class="container-fluid">
        <div class="p-4 border rounded">
            <table id="JobtblData" class="table table-striped table-bordered" style="width:100%">
                <thead class="thead-dark">
                    <tr class="table-info">
                        <th>Job Number</th>
                        <th>Assign From</th>
                        <th>Assign To</th>
                        <th>Date Assign</th>
                        <th>Job Action</th>
                        <th>Remarks</th>
                        <th>Type</th>
                        <th>Division</th>


                    </tr>
                </thead>
            </table>
        </div>
    </div>
</section>


@section Scripts{

    <script src="~/js/cmjobprogress.js"></script>
}
ApplicationDbContext.cs

namespace LXG.DataAccess.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseOracle(@"User Id=xxxxx;Password=xxxxx;Data Source=xx.xx.xx.xx:xxxx/xxxxx;");

            }
        }

       
        public DbSet<CMJobProgress> CMJOBPROG { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {


            modelBuilder.Entity<CMJobProgress>(builder =>
            {
                builder.HasNoKey();
                builder.ToTable("CMJOBPROG");
            });
        }
    }
}
namespace LXG.DataAccess.Data
{
公共类ApplicationDbContext:DbContext
{
公共应用程序DBContext(DbContextOptions)

请求URL:https://localhost:44382/Admin/CMJobProgress/GetAll?_=1602720362984 请求方法:获取 状态代码:400 远程地址:[::1]:44382 推荐人政策:交叉来源时严格来源


有人能帮我吗?

根据您的控制器代码,我发现您已经为api调用启用了ValidateAntiForgeryToken

但是datatable的ajax调用不会将AntiForgery令牌作为头发送到api,因此它会显示400错误

要解决此问题,应在视图中添加
@Html.AntiForgeryToken()
,并修改cmjobprogress.js以获取令牌并将其发送到api

更多详细信息,请参考以下代码:

视图:

@{
Layout=“~/Views/Shared/_Layout.cshtml”;
}
就业行动
  • 创造新工作 工作编号 分配自 分配给 日期分配 就业行动 评论 类型 分部 @Html.AntiForgeryToken() @节脚本{ }
    cmjobprogress.js:

    <script>
    
        var JobdataTable;
    
        $(document).ready(function () {
            loadJobDataTable();
        });
    
        function loadJobDataTable() {
            JobdataTable = $('#JobtblData').DataTable({
                "ajax": {
                    "url": "/Admin/CMJobProgress/GetAll"
                    'beforeSend': function (request) {
                        request.setRequestHeader("RequestVerificationToken", document.getElementsByName('__RequestVerificationToken')[0].value);
                    }
                 },
                "columns": [
                    { "data": "job_number" },
                    { "data": "assign_from" },
                    { "data": "assign)to" },
                    { "data": "date_assign" },
                    { "data": "job_action" },
                    { "data": "remarks" },
                    { "data": "type" },
                    { "data": "division" }
                ]
    
            })
        }
    </script>
    
    
    var-JobdataTable;
    $(文档).ready(函数(){
    loadJobDataTable();
    });
    函数loadJobDataTable(){
    JobdataTable=$('#JobtblData')。数据表({
    “ajax”:{
    “url”:“/Admin/CMJobProgress/GetAll”
    “beforeSend”:函数(请求){
    request.setRequestHeader(“RequestVerificationToken”,document.getElementsByName(“RequestVerificationToken”)[0]。值);
    }
    },
    “栏目”:[
    {“数据”:“作业编号”},
    {“数据”:“分配来自”},
    {“数据”:“分配给”},
    {“数据”:“日期分配”},
    {“数据”:“作业操作”},
    {“数据”:“备注”},
    {“数据”:“类型”},
    {“数据”:“划分”}
    ]
    })
    }
    

    感谢您的回复Jaromanda和Brando。现在我得到了一些列数据,但由于上面的错误,有些我没有得到。实际上如何定义ajax.datatable的列数据名?它实际上与模型属性名不同。

    400如果请求语法格式错误、请求消息帧无效或错误,则表示错误eceptive请求路由-您是否可以在不使用
    [ValidateAntiForgeryToken]
    的情况下进行尝试,如果这样可以解决问题,请阅读System.InvalidCastException:“指定的强制转换无效”。[External code]LXG.DataAccess.Repository.Repository.GetALL(System.Linq.Expressions.Expression,System.Func,string)在CMJobProgressController.cs[外部代码]中的Repository.cs LXG.Areas.Admin.Controllers.CMJobProgressController.GetAll()中我试过了,我又犯了一个错误。在我看来,这个问题与线程标题无关。我建议你可以尝试标记正确的答案,创建一个新问题,并讨论新问题。这样其他面临相同问题的人可以更容易地找到问题并回答。谢谢。白兰度注意到。谢谢。我很高兴你能创建新线程。有关如何标记为答复的更多详细信息,请参阅。
    @{
    
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0 text-dark">Job Action</h1>
                </div><!-- /.col -->
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="cmd.html">Home</a></li>
                    </ol>
                </div><!-- /.col -->
            </div><!-- /.row -->
            <div class="row mb-2">
                <div class="col-6 text-right">
                </div>
                <div class="col-6 text-right">
                    <a class="btn btn-primary"><i class="fas fa-plus"></i> &nbsp; Create New Job</a>
                </div>
            </div>
    
        </div><!-- /.container-fluid -->
    </div>
    
    <section class="content">
        <div class="container-fluid">
            <div class="p-4 border rounded">
                <table id="JobtblData" class="table table-striped table-bordered" style="width:100%">
                    <thead class="thead-dark">
                        <tr class="table-info">
                            <th>Job Number</th>
                            <th>Assign From</th>
                            <th>Assign To</th>
                            <th>Date Assign</th>
                            <th>Job Action</th>
                            <th>Remarks</th>
                            <th>Type</th>
                            <th>Division</th>
    
    
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </section>
    @Html.AntiForgeryToken()
    
    @section Scripts{
    
        <script src="~/js/cmjobprogress.js"></script>
    }
    
    <script>
    
        var JobdataTable;
    
        $(document).ready(function () {
            loadJobDataTable();
        });
    
        function loadJobDataTable() {
            JobdataTable = $('#JobtblData').DataTable({
                "ajax": {
                    "url": "/Admin/CMJobProgress/GetAll"
                    'beforeSend': function (request) {
                        request.setRequestHeader("RequestVerificationToken", document.getElementsByName('__RequestVerificationToken')[0].value);
                    }
                 },
                "columns": [
                    { "data": "job_number" },
                    { "data": "assign_from" },
                    { "data": "assign)to" },
                    { "data": "date_assign" },
                    { "data": "job_action" },
                    { "data": "remarks" },
                    { "data": "type" },
                    { "data": "division" }
                ]
    
            })
        }
    </script>
    
    DataTables warning: table id=tblData - Requested unknown parameter 'jobnumber' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4