Solved

Mvc Ajax Call returning Json result

Posted on 2010-08-26
3
739 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

786 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