6

I'm trying to get a list with an AJAX call to a C# method and display its items with jQuery, but I'm not able to do it. Here is what I got:

public string test()
{
    return "test ok";            
}

$.ajax({
    type: "POST",
    url: "Computer/test",
    success: function (data) {
        alert(data);
    },
    error: function () {
        alert("error");
    }
});

This works as expected and i get an alert with 'test ok' string. However if i try to return a list i'm unable to traverse it in jquery.

public List<string> testList()
{
    List<string> test = new List<string>;
    test.Add("test1");
    test.Add("test2");
    return test;
}

$.ajax({
    type: "POST",
    url: "Computer/testList",
    dataType: "json",
    success: function (data) {
        var list = data.d;
        $.each(list, function (index, item) {
            alert(item);
        });
    },
    error: function (xhr) {
        alert(xhr.responseText);               
    }
});

With this code I get the following error:

System.Collections.Generic.List`1[System.String]

Hope you can help me, thanks.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
Ricardo Soares
  • 345
  • 2
  • 5
  • 9

4 Answers4

11

Use Json on server side with JsonRequestBehavior.AllowGet, check out the reason why we have to use JsonRequestBehavior at Why is JsonRequestBehavior needed?:

public JsonResult testList()
{
    List<string> test = new List<string>;
    test.Add("test1");
    test.Add("test2");
    return Json(test,JsonRequestBehavior.AllowGet);
}

You JS:

$.ajax({
    type: "POST",
    url: "Computer/testList",
    dataType: "json"
})
.done(function(data){
   var list = data;
   $.each(list, function (index, item) {
       alert(item);
   });
})
.fail(function(xhr){
    alert(xhr.responseText); 
});

success and error are deprecated, use .done and fail instead

Community
  • 1
  • 1
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • He is using settings object and defines properties success and error. Those are not deprecated. What is deprecated are methods jqXHR.success() and jqXHR.error() on jqXHR object. This is a return value of `$.ajax()` – Lost_Cause Nov 02 '13 at 04:22
  • @Lost_Cause: yes, I think I need to reword that. `.done` is prefered to `success`. http://stackoverflow.com/questions/8840257/jquery-ajax-handling-continue-responses-success-vs-done – Khanh TO Nov 02 '13 at 04:48
1

Change your controller action to return Json:

public JsonResult testList()
{
    List<string> test = new List<string>;
    test.Add("test1");
    test.Add("test2");
    return Json(test);
}
Lost_Cause
  • 96
  • 2
1

Try using a return type of ActionResult that returns JSON data via the Json() method, like this:

public ActionResult testList()
{
    List<string> test = new List<string>;
    test.Add("test1");
    test.Add("test2");

    return Json(test);
}

Note: In your jQuery .ajax() call you will not need the data.d syntax anymore either, so your jQuery will look like this:

$.ajax({
    type: "POST",
    url: "Computer/testList",
    dataType: "json",
    success: function (data) {
        var list = data;
        $.each(list, function (index, item) {
            alert(item);
        });
    },
    error: function (xhr) {
        alert(xhr.responseText);               
    }
});
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

First of all use JsonResult in your C# code. JsonResult are more reliable here in ASP.NET MVC to return the data.

Simply use dictionary in c# to return collection of KeyValuePair

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79