0

我似乎无法从 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 );
    }
4

2 回答 2

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 语句更改为return Json( result ,JsonRequestBehavior.AllowGet);您的代码然后转到http://yoursite/controllername/GetDescriptions?incomingProjectName=projName查看返回的 json。

于 2012-08-22T13:31:05.720 回答
-1

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

for(var m in msg)
{
    alert(m[i]);
}
于 2012-08-22T14:02:43.660 回答