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

时间:2021-11-07 15:29:52

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编码)并将其发送到客户端并在那里解包?

1 个解决方案

#1


2  

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, i.e. 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
    }
});

#1


2  

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, i.e. 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
    }
});