Friday, October 26, 2012

JQuery getJSON call to MVC Controller


This can happen if the data return by the controller method is violating JSON format. You can try the following-
  • Change ActionResult to JsonResult.
  • Add [AcceptVerbs(HttpVerbs.Get)] to Filter method.
  • Add {} as second paramenter of getJSON as it takes three parameter.
Its working fine with the following code-
    [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult Filter()
    {
       var myData = new[] { new { first = "Murli", last = "D" }, new { first = "Deepak", last = "Tripathi" } };
       return Json(myData, JsonRequestBehavior.AllowGet);
    }
<script type="text/javascript">
$(document).ready(
    function () {
        $.getJSON(
        "/Account/Filter", {}, 
        function (myData) {
                alert(myData);
        });
});
</script>

No comments:

Post a Comment