无法提取JSONResult

时间:2012-08-22 13:22:35

标签: c# javascript json asp.net-mvc-2

我似乎无法从JSONResult中解压缩字符串列表。

这是控制器:

    [HttpPost]
    public JsonResult GetDescriptions(string incomingProjectName)
    {
        List<string> result = new List<string>();
        using (SFEntities ctx = new SFEntities())
        {
            result = (from ct in ctx.SF_CLIENT_TASK
                      join cp in ctx.SF_CLIENT_PROJECT on ct.PROJECT equals cp.ID
                      where cp.NAMEX == incomingProjectName
                      select ct.DESCRIPTION).ToList();
        }

        return Json( result );
    }

这是我的ajax方法:

    $.ajax({
        type: "POST",
        url: "Home/GetDescriptions",
        contentType: "application/json; charset=utf-8",
        data: '{incomingProjectName : "projName"}',
        dataType: "json",
        success: function (msg) {
            alert("msg: " + msg);  // [Object object]
            alert("msg: 2 string: " + msg.toString);    // function toString() { [native code] }
            var list = eval(msg);
            alert("list: " + list); // blank
            alert("list to string: " + list.toString);  // function toString() { [native code] }
            alert("list data: " + list.valueOf);    // valueOf() { [native code] }
            alert("msg[0]: " + msg[0]);     // undefined
            alert("list[0]: " + list[0]);   // undefined
        },
    });

在调试器中,我可以看到结果的内容有很长的项目列表,但我似乎不是它们,但是我尝试在JavaScript中访问它。

是否有某种反序列化或提取方法我无法调用?

编辑:这似乎应该是一个样板的东西,但我还没有能够谷歌许多这样做的例子出于某种原因......

编辑:这是我的最终(工作)代码:

    $.ajax({
        type: "POST",
        url: "Home/GetDescriptions",
        data: { incomingProjectName: projName },
        success: function (msg) {
            alert( "msg: " + msg );
        },
        error: function (msg) {
            alert("Failed: " + msg.status + ": " + msg.statusText);
        }

这是控制器代码:

    public JsonResult GetDescriptions(string incomingProjectName)
    {
        List<string> result = new List<string>();
        using (SFEntities ctx = new SFEntities())
        {
            result = (from ct in ctx.SF_CLIENT_TASK
                      join cp in ctx.SF_CLIENT_PROJECT on ct.PROJECT equals cp.ID
                      where cp.NAMEX == incomingProjectName
                      select ct.DESCRIPTION).ToList();
        }

        return Json( result );
    }

2 个答案:

答案 0 :(得分:1)

我已使用以下控制器方法测试了您的代码,并且它没有任何问题。

public JsonResult Test()
{
    return Json(new List<string> {"a", "b"},JsonRequestBehavior.AllowGet);
}

JS代码:

$.ajax({
    type: "POST",
    url: "Home/Test",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        console.log("msg: " + msg);  // [Object object]
        console.log("msg: 2 string: " + msg.toString);    // function toString() { [native code] }
        var list = eval(msg);
        console.log("list: " + list); // blank on your code
        console.log("list to string: " + list.toString);  // function toString() { [native code] }
        console.log("list data: " + list.valueOf);    // valueOf() { [native code] }
        console.log("msg[0]: " + msg[0]);     // undefined on your code
        console.log("list[0]: " + list[0]);   // undefined on your code
    }
});

成功方法的结果:

msg: a,b
msg: 2 string: function toString() { [native code] }
list: a,b
list to string: function toString() { [native code] }
list data: function valueOf() { [native code] }
msg[0]: a
list[0]: a

我认为问题是您的列表为空或序列化程序存在问题。尝试在调试模式下查看Json结果内容。

更新

要查看从您的控制器操作返回的数据,请删除[HttpPost]属性并将您的返回语句更改为代码上的return Json( result ,JsonRequestBehavior.AllowGet);然后转到http://yoursite/controllername/GetDescriptions?incomingProjectName=projName以查看返回的json。

答案 1 :(得分:-1)

我会删除eval function,为什么会这样?如果你有数组,你应该能够像这样迭代它:

for(var m in msg)
{
    alert(m[i]);
}