JSON和ASP.NET MVC [英] JSON and ASP.NET MVC

查看:98
本文介绍了JSON和ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过一个AJAX返回一个序列化JSON对象来使用ASP.NET MVC客户端调用?

How do you return a serialized JSON object to the client side using ASP.NET MVC via an AJAX call?

推荐答案

从控制器,你可以只返回一个JsonResult:

From the controller you can just return a JsonResult:

public ActionResult MyAction()
{
    ... // Populate myObject
    return new JsonResult{ Data = myObject };
}

Ajax调用的形式将取决于你使用这当然库。使用jQuery它会是这样的:

The form of the Ajax call will depend on which library you're using, of course. Using jQuery it would be something like:

$.getJSON("/controllerName/MyAction", callbackFunction);

其中 callbackFunction参数需要一个参数,这是从XHR请求中的数据。

where the callbackFunction takes a parameter which is the data from the XHR request.

这篇关于JSON和ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆