[英]How do I decode IEnumerable<Person> passed from controller via AJAX to javascript
Controller Action: 控制器动作:
[HttpPost]
public IEnumerable<Person> GetPeopleByUserIdAndYear(int userId, int year)
{
return DataProvider.GetPeople(userId, year).AsEnumerable();
}
Javascript : Javascript:
$.ajax({
cache: false,
type: "POST",
url: '@Url.Action("GetPeopleByUserIdAndYear", "Controller")',
data: { userId: uId, year:selectedYear },
success: function ( data, textStatus, jqXHR ) {
debugger;
//How do I assign the data variable to a javascript array of some sorts?
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus + '\n' + errorThrown);
}
});
So my question is. 所以我的问题是。 The IEnumerable that is returned to the data variable I need to assign to a javascript array.
返回到我需要分配给javascript数组的数据变量的IEnumerable。 How do I do that?
我怎么做?
if i understood you well... if you get JSON
in response then use var someVar = JSON.parse(data)
. 如果我理解得很好...如果您得到
JSON
响应,请使用var someVar = JSON.parse(data)
。 if data
is already serialized array, then just use it ;) 如果
data
已经是序列化数组,那么就使用它;)
You can do something like this in your success block of AJAX: 您可以在AJAX的成功案例中执行以下操作:
for (var i = 0; i < data.length; i++) {
persons.push(data[i]);
}
where persons
is an array. persons
是一个数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.