gpt4 book ai didi

jquery - 将数据从 JQuery 传递到 Controller 操作

转载 作者:行者123 更新时间:2023-12-01 06:42:43 25 4
gpt4 key购买 nike

我是否需要对 Controller 操作方法执行任何特殊操作才能接受/绑定(bind)来自 .ajax() 调用的数据?

Controller 操作 -

public class TestController : Controller
{
public JsonResult GetTestJsonData(Metadata data)
{
return new JsonResult { Data = data };
}
}

JQuery 调用 -

<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "GetTestJsonData",
data: "{ data: {Name:'joe'}}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert("Name = " + msg.Name);
}
});
});
</script>

我希望此页面会以名称“joe”进行提醒。我将 json 数据从客户端传递到 Controller 操作,然后该操作将其传回客户端。这只是我正在尝试做的事情的一个简单示例。

最佳答案

您的代码可能适用于一个简单的示例,因为您没有解析任何内容,但是如果您想将 JSON 转换为 Controller 操作上的真实对象,那么您需要告诉 MVC 如何进行操作。

有一篇关于编写 JsonFilterAttribute 的博客文章,它使用 Json.NET 反序列化传入的 JSON 并将其转换为对象。

http://blogger.forgottenskies.com/?p=252

关于jquery - 将数据从 JQuery 传递到 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/659386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com