在反应中使用fetch时的null [FormBody]字符串值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在反应中使用fetch时的null [FormBody]字符串值相关的知识,希望对你有一定的参考价值。

我在.js文件中有以下提取

    fetch('api/SampleData/GetBpmnXml', {
        method: 'post',

        body: {
            "first_name": "Bob"
        }
    })

在API我有这个帖子

    [HttpPost]
    public void Post([FromBody] string first_name)
    { return first_name;}

为什么返回值为null?而不是鲍勃?

答案

请参阅body部分,参数名称为first_name,而不是api端点中的value。可能会将您的Api端点更改为以下;这样它就可以绑定到参数

[HttpPost]
public void Post([FromBody] string first_name)
{ return value;}

以上是关于在反应中使用fetch时的null [FormBody]字符串值的主要内容,如果未能解决你的问题,请参考以下文章