Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Mvc Ajax Call returning Json result

Posted on 2010-08-26
3
Medium Priority
?
746 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

715 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