C# 检索$.ajax中的值
我想在$.ajax中检索我的对象值 我的web服务方法Statistic_1返回一个包含object2数组的Object1 在C代码中,我以如下方式检索:Object1.Items[0]。例如Name Items是my Object1的属性,它是object2的数组 以下是我的JavaScript代码:C# 检索$.ajax中的值,c#,javascript,jquery,ajax,C#,Javascript,Jquery,Ajax,我想在$.ajax中检索我的对象值 我的web服务方法Statistic_1返回一个包含object2数组的Object1 在C代码中,我以如下方式检索:Object1.Items[0]。例如Name Items是my Object1的属性,它是object2的数组 以下是我的JavaScript代码: function getStatistic1() { var response; var allstat1 = []; $.ajax({ type: 'GET', u
function getStatistic1() {
var response;
var allstat1 = [];
$.ajax({
type: 'GET',
url: 'http://localhost:52768/Service1/Statistic_1',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
response = msg.d;
for (var i = 0; i < response.length; i++) {
allstat1[i] =**???**
}
fillData(allstat1);
},
error: function (e) {
alert("error loading statistic 1");
}
})
}
function fillData(data) {
$('#table_campaigns').dataTable({
**???**
});
}
反应性1级
public class ResponseStatistic_1 : IBaseClientEntity
{
public ResponseStatistic_1()
{
}
public ResponseStatistic_1(Statistic_1 [] items) : this()
{
this.Items = items;
}
#region Properties
public Statistic_1[] Items
{
get;
set;
}
统计1是类
public class Statistic_1
{
private string _geografisch_zone;
private decimal[] _sum;
private int _yearStart;
private int _yearEnd;
...
}
如何实现for循环?取决于您的响应对象 如果返回的是数组:
function successFunction(data) {
for (var i = 0; i < data.length; i++) {
$('#table_campaigns').dataTable().fnAddData(data[i])
}
}
你的json是什么样子的?响应对象是什么样子的?可能是@RoyiNamir和TomCammann的重复。我把代码放在了答案中。我不明白,因为我认为我返回的不是数组。这是一个具有其他对象数组的对象。我把代码放在了答案中。我尝试了这个:response=msg.Items。但响应是空的,为什么?但是,在我的WCFTestClient.exe中,方法Statistic_1运行良好。
function successFunction(data) {
for (var i = 0; i < data.length; i++) {
$('#table_campaigns').dataTable().fnAddData(data[i])
}
}
$.ajax({
type: 'GET',
url: 'http://localhost:52768/Service1/Statistic_1',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: successFunction,
error: function (e) {
alert("error loading statistic 1");
}
})
}