Deepanjan Nag
Deepanjan Nag

Reputation: 921

$.getJSON() not working in ASP.NET MVC4

This is truly weird and I do not know where I am going wrong.

Here's my controller:

    namespace MvcAJAX.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }        

        public ActionResult Called()
        {
            var retObject = new { rollNo = 2, name = "Deepanjan" };
            return Json(retObject);

        }
    }    
}

Here's my Index View:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<script>
    /// <reference path="~/Scripts/jquery-1.7.1.js" />
    $(document).ready(function () {
        $('#button1').click(function () {
            $.getJSON("/Home/Called",function () {
                alert('Working!');
                $('#p1').text("Roll Number was " + data.rollNo + " and the Name was " + data.name);
            });
        });
    });
</script>
<h1>This page demos Load with callback function!</h1>
<input type="button" id="button1" value="Click Me!"/><br />
<p style="height:200px" id="p1"></p>
<hr />

My getJSON simply doesn't work & I do not even get the alert. What's wrong?

Upvotes: 0

Views: 2449

Answers (3)

Hasnain Mehmood
Hasnain Mehmood

Reputation: 417


***> only write in your Mvc controller function this line

Configuration.ProxyCreationEnabled = false;


Upvotes: 0

yusuf
yusuf

Reputation: 1263

Where do you include jquery file?? Jquery file has to first than you can write your own javascript codes..

and try this codes;

$(document).ready(function () {
    $('#button1').click(function () {
        $.getJSON("/Home/Called", function (retObject) {
            alert('Working!');
            $('#p1').text("Roll Number was " + retObject.rollNo + " and the Name was " + retObject.name);
        });
    });
});

Try replacing return Json(retObject); with return Json(retObject,JsonRequestBehavior.AllowGet);

Upvotes: 1

Sebin Eapen George
Sebin Eapen George

Reputation: 215

Try an Ajax post from view , like this

 $.ajax({
 url: '@Url.Action(action)',
  data: data
  type: 'POST',
  success: function (result) {

                              }
                          });

Upvotes: 1

Related Questions