gpt4 book ai didi

jquery - 将 2 个对象数组从 jquery 发送到 MVC Controller

转载 作者:行者123 更新时间:2023-12-01 04:00:17 25 4
gpt4 key购买 nike

我在 Controller 上有以下方法:

public ActionResult TestMultipleParameters(IEnumerable<TestM> competences, IEnumerable<long> extracompetences)
{
return Json("success", JsonRequestBehavior.AllowGet);
}

对象是:

public class TestM
{
public string uid { get; set; }
public long Id { get; set; }
public bool IsFromParent { get; set; }
}

并且从查询中我尝试发送此信息:

var competences = [ {uid: "a", Id: 1, IsFromParent: true}, {uid: "b", Id: 2, IsFromParent: false}];
var extraCompetences = [1, 2, 3];
jQuery.ajaxSettings.traditional = true;
$.ajax({
url: ...,
async: true,
data: JSON.stringify({ competences: competences, extracompetences: extracompetences }),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'GET',
error: handleException,
success: function(data){
console.log("success");
}
})

我尝试了 JSON.stringify 和非 stringify 之间的多种组合,但数据未发送到 MVC。如果没有 JSON.stringify,数组将发送到 Controller ,但 TestM 的值是默认值。

最佳答案

您进行了 GET。 GET 没有正文,您的 contentType 选项将被忽略。

您可以对用 [HttpPost] 修饰的方法进行 POST,或者您需要使用索引器生成名称/值对,但是数据将作为查询字符串发送,并且您可能会超出查询字符串限制并引发异常。

如果进行 GET,则数据 需要采用以下格式

data: { competences[0].uid: "a", competences[0].Id: 1, ..... competences[1].uid: "b", .... },

关于jquery - 将 2 个对象数组从 jquery 发送到 MVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47034438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com