且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

控制器动作中的AJAX空参数

更新时间:2023-02-25 17:07:38

您需要进行两项更改.您确实需要在参数上使用 [FromBody] :

You need to make two changes. You do need [FromBody] on the param:

public IActionResult GetTest([FromBody]string data)

然后,您只需要将字符串作为JSON发送:

Then, you need to send just the string, as JSON:

data: JSON.stringify("test")

这似乎有些怪异,但是如果没有 JSON.stringify ,它将作为 test 发送,并且您需要"test" .简而言之,您要发送的字符串具有JSON内容类型,因此它必须是 JSON字符串.

That may seem a little weird, but without JSON.stringify it will be sent as just test, and you need "test". In short, you're sending a string with a JSON content type, so it must be a JSON string.