简体   繁体   English

将Json对象从服务器端发送到客户端

[英]sending Json object from Server side to the client side

I'm quite new to JSON, I tried to search on the web regarding my question but couldn't find anything, maybe its because the terms are new for me. 我对JSON很新,我试图在网上搜索我的问题,但找不到任何东西,也许是因为这些条款对我来说都是新的。

My question: is there any way / function to wrap a JSON object in the server side (in this case it would be ASP.NET coding on C#) and to send it to the client side and to unwrap it there? 我的问题:是否有任何方法/函数在服务器端包装JSON对象(在这种情况下,它将是C#上的ASP.NET编码)并将其发送到客户端并在那里解包?

In ASP.NET MVC you can return a JsonResult from your controller action, like this: 在ASP.NET MVC中,您可以从控制器操作返回JsonResult,如下所示:

[HttpGet] // or [HttpPost]
public JsonResult MyAction() {
    var object = new MyObject();
    return Json(object);
}

and read from your client function, ie using jQuery, like this: 并从您的客户端函数读取,即使用jQuery,如下所示:

$('mySelector').on('click', function(e) { // 'click' is only an example...
    $.getJSON('MyController/MyAction', {}, function(res) {
        // res contains your JSON result
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM