0

I am trying to load data from controller using ajax.

Controller

[HttpGet] public ActionResult GetServices() { var data = _bbdb.ListServices.Where(d => d.Status == true).ToList(); return Json(data, JsonRequestBehavior.AllowGet); } 

View

<div class="one-third column omega"> <div class="title-wrapper"> <div class="section-title"> <h4 class="title">Services <strong>Availables</strong></h4> </div> <span class="divider"></span> <div class="clear"></div> </div> <ul class="accordion" id="ListServices"> </ul> </div> <div class="clear"></div> <script type="text/javascript"> $(document).ready(function () { $.ajax({ url: '@Url.Action("Home/GetServices")', contentType: "application/json; charset=utf-8", cache: false, global: false, async: false, dataType: "json", success: function (data) { alert(data); $.each(data, function () { $("<li/>").html('<li><div class="parent first"><h6><div class="accordion-caption"></div>' + this.Title + '</h6></div><div class="tcontent"><p>' + this.Description + '</p></div></li>').appendTo("#ListServices"); }); }, error: function (error) { alert(error); } }); }); </script> 

However it does not display the data from the controller, instead it display alert with [object][object].

The returned data should be a list of values used to populate the accordion.

What am I missing here?

Any help will be much appreciated.

4
  • Change that error callback function to this and see what you get so we can narrow down the problem: function(xhr, status, err){ console.log('Response code:' + xhr.status'); console.log('[Error:' + err + '] ' + status); }
    – rwisch45
    CommentedFeb 9, 2014 at 5:09
  • It result in this error Response code:404 [Error:Not Found] error
    – KleberBH
    CommentedFeb 9, 2014 at 5:17
  • An HTTP response 404 means that your url was not found. Double check the url that is being rendered in your Javascript, then double check your routing and make sure everything is as it should be.
    – rwisch45
    CommentedFeb 9, 2014 at 5:23
  • Update, I changed from url: '@Url.Action("Home/GetServices")' to url: 'Home/GetService' and now it is populating the title, however my accordion still not working (sliding down).
    – KleberBH
    CommentedFeb 9, 2014 at 5:24

1 Answer 1

1

Now that you have your URL straight (see comments on OP), lets look at your success callback.

success: function (data) { alert(data); $.each(data, function () { // your html creation here }); }, 

You need to add the parameters to the $.each, like so

$.each(data, function(index, val){ console.log(val.Title); console.log(val.Description); }); 

See jQuery .each() documentation for more info on that.

Also, when you are appending your HTML to your <ul>, all you have to do is this:

$('#ListServices').append('<li><div>Rest of your HTML...</div></li>');

4
  • Ok, it returns a list of values from database. eg.: Andrew Peters Here goes all the description for service.
    – KleberBH
    CommentedFeb 9, 2014 at 5:30
  • @KleberBH So all good? Just replace your this.Title, this.Description, etc in your generated HTML with val.Title, etc
    – rwisch45
    CommentedFeb 9, 2014 at 5:31
  • it is populating the data right, however the accordion still not working, (sliding down to show the description when clicked). I think the problem still on this part $("<li/>").html('<li><div class="parent first"><h6><div class="accordion-caption"></div>' + val.Title + '</h6></div><div class="tcontent"><p>' + val.Description + '</p></div></li>').appendTo("#ListServices");
    – KleberBH
    CommentedFeb 9, 2014 at 5:35
  • @KleberBH Yes, most likely so. That code generates an <li> element, and then sets the HTML of that element to contain an <li>. That leaves you with nested <li> elements. See my edit to the answer
    – rwisch45
    CommentedFeb 9, 2014 at 5:42

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.