javascript - JSON.stringify() 返回 null
问题描述
为什么会typeof(JSON.stringify(outputdata))
在调试控制台中返回一个字符串,而我在后端(.NET 5)中收到 null?
后端部分:
[HttpPost]
[Route("createpost")]
public async Task<IActionResult> CreatePost([FromBody] Article article)
{
Console.WriteLine("CONTENT: " + article.Content);
return Json(article);
}
outputData
是我的 JSON
Article.cs 模型:
public class Article
{
public string Content { get; set; }
}
以下代码是我的 POST 请求:
fetch('/createpost', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
Content : JSON.stringify(this.outputData)
})
});
Content : JSON.stringify(` ${this.outputData} `)
返回未定义
解决方案
它已经被第一个字符串化JSON.stringify
,第二个将字符串化的 json 视为字符串。
尝试:
fetch('/createpost', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
Content : this.outputData
})
});
推荐阅读
- kotlin - 为什么 Kotlin 密封类无法实现子类型函数重载?
- java - 避免从 AWS S3 进行多次下载
- php - 带有用户和答案的 PHP 测验系统
- php - utf-8 Carbon 格式的错误字符本地化
- regex - 冒号分隔符的正则表达式语法
- php - Symfony\Component\Debug\Exception\FatalThrowableError 解析错误:语法错误,意外 'const' (T_CONST),期望变量 (T_VARIABLE)
- php - 如何使用 foreach PHP 动态地将索引和值附加到数组
- c - `-V` 标志如何既是 `--verbose` 又是 `--version`?
- docker - 将非 dockerised 应用程序连接到 kubenetes pos
- spring - Spring mvc - 带有@ModelAttribute的POST请求导致不必要的sql查询