我知道有些人问了同样的问题并得到了解答。我已经查看了所有这些,但仍然无法解决我的问题。我有一个 jquery snipet,它将值发送到处理程序,处理程序处理来自 JS 的值并将数据作为 JSON 数据返回。 JSON 数据有两组记录(数据库中的两行),需要通过 getJSON 捕获并处理它们。 JSON 数据看起来像
[{"Name":"P1","Description":"pd1",Value":"S1Test1"},{"Name":"P1","Description":"pd1","Value":"L1Test1"}]
我的JS是
$(document).ready(function () {
$.getJSON('ProfileHandler.ashx', { 'ProfileName': 'P1' }, function (data) {
alert(data.Name);
});
});
我的处理程序代码是
string ProfileName = context.Request["ProfileName"];
GetProfileDataService GetProfileDataService = new BokingEngine.MasterDataService.GetProfileDataService();
IEnumerable<ProfileData> ProfileDetails = GetProfileDataService.GetList(new ProfileSearchCriteria { Name = ProfileName });
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
string serProfileDetails = javaScriptSerializer.Serialize(ProfileDetails);
context.Response.ContentType = "text/json";
context.Response.Write(serProfileDetails);
这里的接近错误是什么?
最佳答案
data
是一个对象数组
data[0].name
应该足以获取名字。
要迭代整个数组,您可以执行以下操作:
$.each(data, function(k, v){
alert(v.name);
});
其中v
是数组中的当前对象。请注意,v == this
关于jquery - $.getJSON 返回 “undefined” 或 [object Object] [object Object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15265992/