gpt4 book ai didi

c# - Jquery ajax 响应返回 null

转载 作者:行者123 更新时间:2023-11-28 01:32:00 25 4
gpt4 key购买 nike

我想从 Web 服务方法检索数据,但它返回 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:“对象引用未设置到对象的实例。”, 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;

...
}

最佳答案

您正在返回字符串“OKKKKKK”,但您在$.ajax函数中将数据类型设置为JSON,因此您必须取消设置数据类型或返回类似“{响应:'OKKKK'}”

关于c# - Jquery ajax 响应返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22008900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com