ASP.NET MVC JsonResult

January 7th, 2010.
Filed under Programming.

Quick note on creating and returning JSON via a controler, without using JayRock, which allows clients, typically JavaScript in web pages, to be able to call into server-side methods using JSON as the wire format and JSON-RPC as the procedure invocation protocol. The methods can be called synchronously or asynchronously !

public JsonResult Person()
        {
            Person millad = new Person();
            millad.age = 22;
            millad.name = "Millad";
            // Sometimes it does not allow you to use a GET request, which you have to allow by
           // typing the second parameter as shown below.
            return Json(millad, JsonRequestBehavior.AllowGet);
        }

You can now simply call this data by using JQuery

 $(function () {
            $.getJSON("Home/Person/", "JSON", function (data) {
                console.log( data );
            });

        });

Leave a Reply