【发布时间】:2018-09-22 13:30:54
【问题描述】:
在我的 AJAX 脚本中迭代 C# MVC 框架中控制器返回的对象时遇到问题。
Ajax 脚本:
function getRName(obj) {
$.ajax({
url: 'resourceNamePopup',
method: 'post',
data: { "search": obj.value },
dataType: 'json',
success: function (data) {
alert('success');
$.each(data.items, function (items) {
alert(items.displayName);
});
}
});
}
C# 动作:
public ActionResult resourceNamePopup(string search)
{
List<ResourceName> rnList = new List<ResourceName>();
rnList = getResourceName(search);
return Json(rnList );
}
我现在收到此错误
http://localhost:52273/Scripts/jquery-1.10.2.js 中第 645 行第 4 列未处理的异常
0x800a138f - JavaScript 运行时错误:无法获取未定义或空引用的属性“长度”
【问题讨论】:
-
你为什么返回
List<object>?只需返回rnList。 -
感谢您指出错误。我纠正了它。现在我收到此错误:localhost:52273/Scripts/jquery-1.10.2.js 0x800a138f 第 645 行第 4 列未处理的异常 - JavaScript 运行时错误:无法获取未定义或空引用的属性“长度”
-
你遇到了什么错误?
标签: c# json ajax asp.net-mvc serialization