Solved

Mvc Ajax Call returning Json result

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

738 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