Solved

Mvc Ajax Call returning Json result

Posted on 2010-08-26
3
735 Views
Last Modified: 2013-11-08
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
0
Comment
Question by:gerrie-govaerts
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33530631
Use $.post instead $.getJSON

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

Open in new window

0
 

Author Closing Comment

by:gerrie-govaerts
ID: 33530693
It works, but I don't understand why every tutorial makes use of getJson ?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33530796
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
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There is a huge demand for CodeIgniter among the PHP web developers due to its dynamic features and benefits these days. It is one of most popular and agile open source PHP framework for creating robust web applications in PHP web development field.…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now