AJAX post 字符串数组到 webapi 2

AJAX post string array to webapi 2

这是我的 ajax 电话:

var myIds = ["A","B","C"]
$.ajax({
    type: "POST",
    url: /DoStuffAndThings?year=2018&name=test,
    data: {myIds: myIds},
    traditional: false
});

这是我的控制器操作:

[HttpPost]
public void DoStuffAndThings(int year, string name, [FromBody] List<string> myIds) {
    // do stuff
}

年份和姓名没有问题,但 myIds 始终为空。

我试过了

data: {myIds: myIds}data: myIdsdata: {"": myIds} 我尝试使用 Ienumerable<string>List<string>string[]

我试过传统的:真假

向网络服务器发送数据时,数据必须是字符串。 使用 JSON.stringify().

将 JavaScript 对象转换为字符串

在数据之前,使用JSON.stringify(myIds)

 var myIds = ["A","B","C"]
  $.ajax({
       type: "POST",
       contentType: "application/json",
       dataType: 'json',
       url: /DoStuffAndThings?year=2018&name=test,
       data: JSON.stringify(myIds),
       traditional: false
 });

模型绑定器无法解析发送数据,因为它不知道格式

使用JSON.stringify以及相应的参数

var myIds = ["A","B","C"];
$.ajax({
    type: "POST",
    url: "/DoStuffAndThings?year=2018&name=test",
    contentType: "application/json",
    dataType: "json",
    data:JSON.stringify(myIds),
    traditional: false
});

模型绑定器随后应该能够识别请求正文中的字符串集合。