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