I want to use jQuery.getJSON function in ASP.NET MVC 3.0, so I wrote the code below for test:
<input id="btn" type="button" />
<script>
$("#btn").click(function () {
$.getJSON("/Location/GetData", null, function (data) {
alert(data);
});
});
</script>
and I have a LocationController with below method:
public JsonResult GetData()
{
List<int> result = new List<int>(){1, 4, 5};
return Json(result);
}
But it doesn’t work!
The GetData method calls, but ‘alert’ is not shown!
You need to tell MVC to allow your JSON action to be called via GETs by changing your return to:
By default (for security reasons) they only allow Json to be requested via POSTs.