What is Json ?
Json stands for javascript object notation which is used to interchange data between various languages a typical example is any server side language like c#,vb.net with Javascript.
How to use Json in MVC ?
well,whenever you make an Ajax Call to retrieve some sort of data then the best format to retrieve data is Json which is string:value pair.
get more about json here
Now lets create a Html to Make an Ajax call and retrieve Json.
What time is it?
<% using (Ajax.BeginForm("GetTime", new AjaxOptions { UpdateTargetId = "myResults" })) { %> Show me the time in: <% } %>Results will appear hereThis page was generated at <%= DateTime.UtcNow.ToString("h:MM:ss tt") %> (UTC)Confirmation Dialog..
which looks like as follows :
Now my Javascript with making ajax call is as follows :
The Main thing for this post is $.Ajax with Get and Post xmlHttpRequests.
on successful response its get displayed else response received with an Error like internal server error (500) etc.
following action receives the submit for this page with an Ajax Call.
public JsonResult GetTime(string zone)
{
Person person = new Person();
person.Name = "Vishal";
person.Address = "Indore";
return Json(person);
}
Which returns the data in Json Format.
When I use xmlhttprequest Post in $.Ajax function of Jquery I receive the following Response.
and When I use xmlhttprequest type Get in $.Ajax function nothing is updated in Red dotted area and I receive internal server Error (500) with following Error Message as Response.
This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.
Its because Json only allow Json Post(as its more secure) Request but deny Get request because confidential information can not be retrieve using get request.
but if you aware its risk and want to allow Get request for Json you can do this as follows :
return Json(person,JsonRequestBehavior.AllowGet);
in this way you can retrieve data from Get Request but its won't be secure anymore as anyone can now access this infromation by simply sending request using url.


