使用 $.each 迭代 ajax 返回的列表 - 元素始终为空
Iterating over a ajax-returned list with $.each - Element is always null
我的 API-controller 中有以下 C# 代码:
public JsonResult GetProjects()
{
List<ProjectItem> list = ProjectItem.Get();
return new JsonResult() {Data = list, JsonRequestBehavior = JsonRequestBehavior.AllowGet};;
}
我逐步检查了这段代码,发现列表中包含关于这一点的元素。但是在 Ajax 调用中,我调用的代码尝试使用 Jquery 遍历单个元素并尝试使用它们。但是元素总是空的。
Typescript Ajax-调用:
$.ajax({
url: "/api//MyControllerName",
type: "GET",
cache:false,
dataType: "json",
success: (result) => {
base.projects.removeAll();
$.each(result, (index: number, element: any) => {
//Here the element from the list should be used, but it is always null
});
});
}
});
这段代码有什么问题?
好的,我自己找到了问题的解决方案:
我试图遍历结果,但我必须遍历结果的Data-属性。
此代码有效:
$.each(result.Data, (index: number, element: any) => {
//Element is defined
}
我的 API-controller 中有以下 C# 代码:
public JsonResult GetProjects()
{
List<ProjectItem> list = ProjectItem.Get();
return new JsonResult() {Data = list, JsonRequestBehavior = JsonRequestBehavior.AllowGet};;
}
我逐步检查了这段代码,发现列表中包含关于这一点的元素。但是在 Ajax 调用中,我调用的代码尝试使用 Jquery 遍历单个元素并尝试使用它们。但是元素总是空的。
Typescript Ajax-调用:
$.ajax({
url: "/api//MyControllerName",
type: "GET",
cache:false,
dataType: "json",
success: (result) => {
base.projects.removeAll();
$.each(result, (index: number, element: any) => {
//Here the element from the list should be used, but it is always null
});
});
}
});
这段代码有什么问题?
好的,我自己找到了问题的解决方案:
我试图遍历结果,但我必须遍历结果的Data-属性。
此代码有效:
$.each(result.Data, (index: number, element: any) => {
//Element is defined
}