使用vanilla Javascript向ASP.net api控制器发送字符串列表
使用普通Javascript向ASP.net核心api控制器发送字符串列表 这听起来可能很简单,但有些地方出了问题,我不确定是什么。我正在尝试向ASP.Net Core中的api post控制器发送字符串列表,例如使用vanilla Javascript向ASP.net api控制器发送字符串列表,javascript,asp.net,json,asp.net-core,asp.net-web-api,Javascript,Asp.net,Json,Asp.net Core,Asp.net Web Api,使用普通Javascript向ASP.net核心api控制器发送字符串列表 这听起来可能很简单,但有些地方出了问题,我不确定是什么。我正在尝试向ASP.Net Core中的api post控制器发送字符串列表,例如[“某物”、“其他东西”、“另一个字符串”] 我的控制器看起来像这样 [HttpPost] public ActionResult<MyModel> Name([FromBody] List<string> list) {
[“某物”、“其他东西”、“另一个字符串”]
我的控制器看起来像这样
[HttpPost]
public ActionResult<MyModel> Name([FromBody] List<string> list)
{
// Do something...
return NoContent();
}
async function apiCall() {
const response = await fetch("URL", {
method: 'POST',
headers: {
'Content-type': 'application/json',
'X-CSRFToken': csrftoken
},
body: JSON.stringify({ list: [ "something", "something else", "Another string" ] })
}}
每当我调用apiCall()
函数时,它都会将数据发布到控制器,但数据列表始终为空。如何正确发布此数据?更改
body: JSON.stringify({ list: [ "something", "something else", "Another string" ] })
到
它是有效的。我发送的不是字符串数组,而是一个以字符串数组作为属性的对象。您的模型不是字符串数组,而是一个以字符串数组作为属性的对象,非常感谢这是一个简单的修复。当我将主体更改为body:JSON.stringify([“something”、“something other”、“other string”])
时,它就工作了!有时会发生;)
body: JSON.stringify([ "something", "something else", "Another string" ])