MoshMosh
MoshMosh

Reputation: 71

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.

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?

Upvotes: 0

Views: 2016

Answers (1)

Flea777
Flea777

Reputation: 1022

In ASP.NET MVC you can return a JsonResult from your controller action, like this:

[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:

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

Upvotes: 2

Related Questions