Skip to main content

Posts

Showing posts with the label Json

Asp.net mvc using JsonResult

Json has become one of the most popular form of data interchange method and being tightly integrated in the mvc framework makes it very easy to use. I will be demonstratating a very usefull technique of passing a response in an Ajax style using JsonResult (It is a class that is used in mvc framework to pass a json formatted content to the response). Model public class Users { public int UserId { get; set; } public string UserName { get; set; } } Controller public ActionResult Index() { return View(); } public JsonResult GetUsers() { List<Users> users = new List<Users>() { new Users { UserId =1, UserName ="kaunain" }}; return this.Json(users,JsonRequestBehavior.AllowGet); } View <script type="text/javascript" language="javascript"...

Asp.net mvc 2 sending Json request to external url and returning value from it.

$(document).ready(function () { $("#Submit1").click(function () { var txtvalue= $("#txtvalue").val(); var txtvalue2= $("#txtvalue2").val(); var urlStr = "&txtvalue=" + txtvalue+ "txtvalue2=" + txtvalue2; $.getJSON("http://localhost:5555/Home/Index?format=json&callback=?" + urlStr, function (data) { alert(data); }); }); }); The important point of note here is that you will not be going to get back any value due to the restriction by the browser to stop the xss without specifying callback=? in the url which will result in JsonP call (not a plain JSON call) which is used for calling the external urls. Method which is going to serve the json request. public ActionResult Index(FormCollection form) { var data = new { Value ="true"}; return Json(data,JsonRequestBehavior.AllowGet); } Use jquery 1.5 or greater version.