【问题标题】:Jquery ajax return null in responseJquery ajax返回null作为响应
【发布时间】: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


    【解决方案1】:

    您正在返回字符串 "OKKKKKK",但您在 $.ajax 函数中将 dataType 设置为 JSON,因此您必须取消设置 dataType 或返回类似 "{response: 'OKKKK'}" 的内容。

    【讨论】:

    • 我不明白,我的 GetTEST() 方法效果很好,问题出在我的 Statistic_1 方法上。你想让我改变什么数据类型?
    • 我在 SqlConnection 的 Statistic_1 方法中有一个异常。例外是:对象引用未设置为对象的实例。你有什么想法吗?
    【解决方案2】:

    请将response = msg.Items;改为response = msg;

    @campsjos 的回答也是这么说的。

    【讨论】:

    • 我编辑了我的帖子,您可以看到 2 个课程。我必须把 msg.Items 没有?因为这是一个对象(这里是 ResponseStatistic_1),它有一个对象数组(这里是 Statistic_1)
    • 我在 SqlConnection 的 Statistic_1 方法中有一个异常。例外是:对象引用未设置为对象的实例。你有什么想法吗?
    【解决方案3】:

    我解决了这个问题。

    我的解决方案中有 2 个项目,WCF 和一个应用程序控制台,可以用来关闭 Web 服务。

    我还在 GettingStartedHost 项目的 app.config 中添加了连接字符串,现在它运行良好。

    【讨论】:

      猜你喜欢
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      相关资源
      最近更新 更多