0

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?

2
  • Check your browser console for any errors. Did you check whether the click event is called? Commented Feb 21, 2013 at 11:26
  • 1
    Try replacing return Json(retObject); with return Json(retObject,JsonRequestBehavior.AllowGet); Commented Feb 21, 2013 at 11:41

3 Answers 3

1

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);

Sign up to request clarification or add additional context in comments.

Comments

1

Try an Ajax post from view , like this

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

                              }
                          });

Comments

0

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

Configuration.ProxyCreationEnabled = false;


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.