Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用vanilla Javascript向ASP.net api控制器发送字符串列表_Javascript_Asp.net_Json_Asp.net Core_Asp.net Web Api - Fatal编程技术网

使用vanilla Javascript向ASP.net api控制器发送字符串列表

使用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) {

使用普通Javascript向ASP.net核心api控制器发送字符串列表

这听起来可能很简单,但有些地方出了问题,我不确定是什么。我正在尝试向ASP.Net Core中的api post控制器发送字符串列表,例如
[“某物”、“其他东西”、“另一个字符串”]

我的控制器看起来像这样

[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" ])