Link to home
Start Free TrialLog in
Avatar of gerrie-govaerts
gerrie-govaertsFlag for Belgium

asked on

Mvc Ajax Call returning Json result

Hello everybody

Why is this code, that returns a Json Result from the controller, incorrect? It does not give me the second alert (call completed). The first alert works well when changing a value in the listbox on my view.

<script type="text/javascript">
        $(document).ready(function () {
            $("#MaterialsList").change(function () {
                alert('changed');
                $.getJSON("/Stock/LoadBrands", null, function (data) {
                    alert('call completed');
                });
            });
        });
</script>

Open in new window


Code in the controller: (I also tried using returning a JsonResult instead of ActionResult)

public ActionResult LoadBrands()
{
            var data = new { name = "Gerrie" };

            return Json(data);[/b]
}

Open in new window


Thx!

Gerrie
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gerrie-govaerts

ASKER

It works, but I don't understand why every tutorial makes use of getJson ?
Thanks a lot for the points!

If you handle the error function (with jQuery.ajax(), you should see clearly what happen, in your case a message worrying about the method)

error : function(data) { alert(data); },

Check the Controller action here :

http://weblogs.asp.net/mikebosch/archive/2008/02/15/asp-net-mvc-submitting-ajax-form-with-jquery.aspx