【问题标题】:ASP Net core ajax not recognizing passed dataASP Net core ajax 无法识别传递的数据
【发布时间】:2019-06-21 07:54:10
【问题描述】:

我的控制器方法看起来像这样

[HttpPost]
public IActionResult CreateNew(string Naslov, int KorisnikID, int GrupaID, string Tekst)
{
    //Some code
}

调用 this 的 Ajax 函数是:

$.ajax({
    type: "POST",
    url: "/Story/CreateNew",
    contentType: "application.json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify({
        "Naslov": $("#Naslov").val(),
        "KorisnikID": @Context.Request.Cookies["kid"],
        "GrupaID": $("#GrupaID").val(),
        "Tekst": $("#Tekst .ql-editor").html()
    }),
    success: function (message) {
        var elements = message.split("-");

        if (elements[0] == "success") {
            window.location.href = '/Clanak?ID' + elements[1];
        }
        else {
            alert(message);
        }
    },
    error: function (xhr, status, error) {
        alert(xhr + " " + status + " " + error);
    }
});

方法已输入,但所有参数均为空。

这是 post 方法在浏览器检查中的样子

当我将其更改为 GET 时,它也可以工作(但我需要发布,因为我的字符串长度有问题)

【问题讨论】:

    标签: asp.net ajax asp.net-core


    【解决方案1】:

    问题是您不能像使用 get 一样使用 POST 方法,您需要向它提供 anti-forgery 令牌。

    可以找到完整的解决方案here

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 2013-11-12
      • 2023-01-30
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      相关资源
      最近更新 更多