Jquery I';我在.NETCore中的ajax调用中发现xsr.send错误400

Jquery I';我在.NETCore中的ajax调用中发现xsr.send错误400,jquery,ajax,.net-core,Jquery,Ajax,.net Core,我尝试在.NETCore2.0中使用实体框架使用jQuery2.2进行Ajax调用时遇到一个错误400 我的控制器方法如下所示: [HttpPost] [ValidateAntiForgeryToken] public JsonResult RetrieveDateCollections(string CollectionDate) { ViewData["CollectionTypeId"] = new SelectList(_con

我尝试在.NETCore2.0中使用实体框架使用jQuery2.2进行Ajax调用时遇到一个错误400

我的控制器方法如下所示:

[HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult RetrieveDateCollections(string CollectionDate)
    {
        ViewData["CollectionTypeId"] = new SelectList(_context.Set<CollectionType>(), "CollectionTypeId", "CollectionTypeDescription");
        ViewData["MemberId"] = new SelectList(_context.Member, "MemberId", "FullName");

        var sPCC_Context = _context.Collection.Include(c => c.CollectionType).Include(c => c.Member);
        return Json(sPCC_Context.ToListAsync());
    }
我从我的角度这样称呼它:

<input type="date" asp-for="CollectionDate" class="form-control" onblur="RetrieveDateCollections();" />

有什么想法吗?

您的代码无法验证反伪造令牌(因为您没有发送它)

您可以删除控制器中的
[ValidateAntiForgeryToken]
标记,或者添加一种在javascript中检索反伪造标记并发布它的方法。最简单的方法是通过以下方式修改JS:

function RetrieveDateCollections() {
    var collectionDate = $('#CollectionDate').val().toString();
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Collections/RetrieveDateCollections",
        timeout: 40000,
        headers: { "RequestVerificationToken": getAntiforgeryToken() },
        data: JSON.stringify({ "CollectionDate": "12/10/2018" }),
        dataType: "json",
        done: function (data) {
            alert("Success: " + response.toString());
        },
        fail: function (Result) {
            alert("Error: " + Result.statusCode);
        }
    });
}

function getAntiforgeryToken() {
    // get the token from the form and return it
    var token = $('input[name="__RequestVerificationToken"]').val();
    return token;
}
为此,您需要在视图中包含一个反伪造令牌,例如:

@Html.AntiForgeryToken()

options.hasContent和options.data的值是什么?它们是否保存了您期望的值?JQuery库中的那行代码I。我不知道它应该做什么。而且,我还没有能够使用“data”参数将信息输入到我的方法中。现在我一直在使用QueryString。不过,我还是深入了解了这个方法。因此,在您的服务器端方法(mvc)中,将它包装在一个try-catch块中,然后查看catch语句中的内容。另外,如果它到达了方法,那么当您执行步骤时,是哪一行导致了错误。我还怀疑您只需要
返回Json(sPCC_上下文)确定。所以更新。只有当我删除“[ValidateAntiForgeryToken]”时,它才能访问该方法。但是我得到了一个错误:“RetrieveDataCollections未能加载资源:net::ERR\u CONNECTION\u RESET”。另外,在方法本身中没有抛出异常。在实现下面的代码之后,我仍然得到连接重置错误。IE中的错误是“XMLHttpRequest:Network error 0x2eff,由于错误00002eff而无法完成操作”。不确定这是否有帮助。在实现上述代码后,我仍然收到连接重置错误。IE中的错误是“XMLHttpRequest:网络错误0x2eff,由于错误00002eff而无法完成操作”。不确定这是否有帮助。更新:最终解决方案是双重的。上面的代码帮助我摆脱了400错误,留下了连接一。正在重置连接,因为显然从服务器端方法返回的数据太多且格式错误。我将“return Json(sPCC_Context.ToListAsync());”替换为“return Json({'CollectionDate':'12/10/2018'})”,调用完成。现在开始以正确的格式获取数据。谢谢大家@Ebarreto正如您所提到的,格式问题是由
扩展名
引起的,至少对我来说是这样。如果您尝试此
var sPCC\u Context=\u Context.Collection它会工作的。我也在试图找到解决办法。如果您找到了解决方法,请告诉我。当我在view/csthtml页面上添加
@Html.AntiForgeryToken()
时,它对我有效。在ajax请求中包含以下代码
beforeSend:function(xhr){xhr.setRequestHeader(“XSRF-TOKEN”,$('input:hidden[name=“\uu RequestVerificationToken”]”).val();},contentType:“application/json;charset=utf-8”,数据类型:“json”,数据:json.stringify(datas),
function RetrieveDateCollections() {
    var collectionDate = $('#CollectionDate').val().toString();
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Collections/RetrieveDateCollections",
        timeout: 40000,
        headers: { "RequestVerificationToken": getAntiforgeryToken() },
        data: JSON.stringify({ "CollectionDate": "12/10/2018" }),
        dataType: "json",
        done: function (data) {
            alert("Success: " + response.toString());
        },
        fail: function (Result) {
            alert("Error: " + Result.statusCode);
        }
    });
}

function getAntiforgeryToken() {
    // get the token from the form and return it
    var token = $('input[name="__RequestVerificationToken"]').val();
    return token;
}
@Html.AntiForgeryToken()