对于 ajax GET,值为空

Values are null for ajax GET

本文关键字:ajax GET 对于      更新时间:2023-09-26

这是行不通的,值在服务器上null

JavaScript:

$.ajax({
    url: '/PlayZone/GetCards',
    type: "GET",
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    data: JSON.stringify({ 'what': what, 'by': by, 'jump': jump }),
});

C#:

[HttpGet]
public ActionResult GetCards(string what ,string by, string jump)
{
    ...
}

这工作正常,但我需要 GET ajax 工作来喜欢这篇文章。 Javascript:

$.ajax({
    url: '/PlayZone/GetCards',
    type: "POST",
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    data: JSON.stringify({ 'what': what, 'by': by, 'jump': jump }),
});

C#

[HttpPost]
public ActionResult GetCards(string what ,string by, string jump)
{
    ...
}

试试这个

              $.ajax({
                    url: '/PlayZone/GetCards',
                    dataType: "json",
                    type: "POST",
                    data: { what: what, by: by, jump: jump },
                    success: function (response) {
                              alert("success");
                    }
                });

只需在 java 脚本中传递如下值

data: {'what': what, 'by': by,'jump': jump }

当使用 Get 作为类型时,将数据传递到服务器的唯一方法是通过 url。

您可以通过执行类似操作来侥幸逃脱:

$.get("/PlayZone/GetCards", {what: what, by: by, jump: jump}).done(function(result) {});

Jquery 会将 javascript 对象更改为查询字符串参数以传递给您的操作。

另一种方法是生成一个包含虚拟数据的查询字符串参数的 URL,然后在该字符串上使用替换来添加值。

就个人而言,我会选择$.get解决方案