小编Ric*_*res的帖子

无法使用来自ajax调用的返回列表

我正在尝试获取一个带有对C#方法的AJAX调用的列表,并使用jQuery显示其项目,但我无法做到.这是我得到的:

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

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

这按预期工作,我得到一个'test ok'字符串的警报.但是,如果我尝试返回一个列表,我无法在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);               
    }
});
Run Code Online (Sandbox Code Playgroud)

使用此代码,我收到以下错误:

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

希望你能帮助我,谢谢.

c# ajax asp.net-mvc jquery

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

ajax ×1

asp.net-mvc ×1

c# ×1

jquery ×1