【发布时间】:2014-02-25 08:49:02
【问题描述】:
我想从 ma Web Service 方法中检索数据,但它返回 null。
我使用返回字符串的方法(我的 Web 服务中的 GetTEST())进行测试,它运行良好。
当我使用 WCFTestClient.exe 进行测试时,Statistic_1 方法运行良好,但使用 JQuery 它返回 null。
这是 JavaScript 代码:
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.Items;
console.log(msg);
for (var i = 0; i < response.length; i++) {
allstat1[i] = [response[i].Geografisch_zone];
}
fillDataTable(allstat1);
},
error: function (e) {
alert("error loading statistic 1");
}
});
}
这是我的 IService1.cs
[DataContractFormatAttribute]
[ServiceContract(Namespace ="WSSage100")]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)]
string GetTEST();
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
ResponseStatistic_1 Statistic_1();
}
这里是Service1.svc
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetTEST()
{
return "OKKKKKKKK";
}
public ResponseStatistic_1 Statistic_1()
{...}
}
这里是“console.log(msg)”显示:Object {ErrorMessage:“Object reference not set to an instance of an object.”, ErrorOccured: true, Items: null, NbRecord: 0}
ResponseSatistic_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;
}
}
还有 Statistic_1 类:
public class Statistic_1
{
private string _geografisch_zone;
private decimal[] _sum;
private int _yearStart;
private int _yearEnd;
...
}
【问题讨论】:
标签: c# javascript jquery ajax