数据表nopcommerce 3.80中不存在必需的反伪造表单字段“ __RequestVerificationToken”

时间:2019-02-15 07:07:58

标签: c# asp.net-mvc antiforgerytoken nopcommerce-3.80

我想获取数据并在nopcommerce 3.80中的datatable中显示。当时,我会显示错误,即必需的防伪表单字段“ __RequestVerificationToken”不存在。我也随时在想要添加的地方添加 antiforgery 关键字控制器并以视图形式显示相同的错误。

这是我的表单视图,

<form id="Master-form" name="Master-form">
@Html.AntiForgeryToken()
    <div class="content">
        <div class="form-horizontal">
            <div class="row">
            </div>
            <div id="product-edit" class="nav-tabs-custom">
                <ul class="nav nav-tabs">
                    @Html.RenderBootstrapTabHeader("tab-country", @T("Admin.Master.Country"), true)
                </ul>
                <div class="tab-content">
                    @Html.RenderBootstrapTabContent("tab-country", @TabCountry(), true)                    
                </div>
            </div>
        </div>
    </div>
</form>

这是我的部分看法

<!-- /.row -->
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                @T("Admin.Master.CountryList")
            </div>
            <!-- /.panel-heading -->
            <div class="panel-body">
                <table id="dataGrid" class="table table-striped table-bordered dt-responsive nowrap" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th>Id</th>
                            <th>Country Name</th>
                            <th>Active</th>
                            <th>Edit</th>
                            <th>Delete</th>
                        </tr>
                    </thead>
                </table>
            </div>
            <!-- /.panel-body -->
        </div>
        <!-- /.panel -->
    </div>
    <!-- /.col-lg-12 -->
</div>

<script>
    $(document).ready(function () {
        $('#dataGrid').DataTable({
            "scrollX": true,  // scrolling horizontal
            "bSort": false,
            "bFilter": false,
            "processing": true, // for show progress bar
            "serverSide": true, // for process server side
            "pageLength": 5,
            "lengthMenu": [5, 10, 50, 100, 1000, 10000],

            "ajax": {
                "url": 'AwazMaster/CountryList',
                "type": "POST",
                "datatype": "json",
                "contentType": "application/json; charset=utf-8"
            },

            "columnDefs":
                [{
                    "targets": [0],
                    "visible": false,
                    "searchable": true,
                    "orderable": false
                }
                ],
            "columns": [
                { "data": "Id", "name": "Id", "autoWidth": true },
                { "data": "CountryName", "name": "service", "autoWidth": true },
                { "data": "IsActive", "name": "active", "autoWidth": true },

                {
                    "render": function (data, type, row) { return "<a class='btn btn-info' onclick=Edit('" + row.Id + "');   >Edit</a>"; }
                },
                {
                    "render": function (data, type, row) {
                        return "<a href='#' class='btn btn-danger' onclick=DeleteData('" + row.Id + "'); >Delete</a>";
                    }
                },
            ]
        });
    });    
</script>

这是我的控制人,

[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult CountryList()
{
    // Getting all data  
    var dataList = _MasterService.CountryList(
        start: Convert.ToInt32(Request.Form["start"]),
        pageSize: Request.Form["length"].ToString() != null ? Convert.ToInt32(Request.Form["length"].ToString()) : 0,
        sortColumnName: Request.Form["columns[" + Request.Form["order[0][column]"] + "][name]"],
        sortColumnDirection: Request.Form["order[0][dir]"]);

    var data = dataList.Select(x => new
    {
        Id = x.Id,
        CountryName = x.CountryName,
        IsActive = x.IsActive,
    });

    //Returning Json Data
    Response.StatusCode = 200;
    return Json(new { draw = Request.Form["draw"], recordsFiltered = dataList.TotalCount, recordsTotal = dataList.TotalCount, data = data });
}

为更清晰起见,上图显示了完整错误 enter image description here

在浏览器控制台中显示 500(内部服务器错误)

2 个答案:

答案 0 :(得分:0)

您必须通过ajax请求传递__RequestVerificationToken内部生成的form,如下所示:

var token = $('input[name="__RequestVerificationToken"]').val();

"ajax": {
    "url": 'AwazMaster/CountryList',
    "type": "POST",
    "headers": {__RequestVerificationToken : token} // <--- here it is
    "datatype": "json",
    "contentType": "application/json; charset=utf-8"
 },

答案 1 :(得分:0)

  beforeSend: function (xhr) {
                xhr.setRequestHeader("XSRF-TOKEN", $('input[name="__RequestVerificationToken"]').val());
            },