简体   繁体   English

jQuery ajax在响应中返回null

[英]Jquery ajax return null in response

I would like retrieve data from ma Web Service method but it returns null. 我想从Web服务方法检索数据,但它返回null。

I test with a method (GetTEST() in my Web Service) that return a string and it works well. 我使用返回字符串的方法(Web服务中的GetTEST())进行测试,并且效果很好。

When I'm testing with WCFTestClient.exe, Statistic_1 method works well, but with JQuery it returns null. 当我使用WCFTestClient.exe进行测试时,Statistic_1方法运行良好,但对于JQuery,它返回null。

Here is JavaScript code : 这是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");
    }
});
}

Here is my IService1.cs 这是我的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();
}

Here is Service1.svc 这是Service1.svc

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{

    public string GetTEST()
    {
        return "OKKKKKKKK";
    }

    public ResponseStatistic_1 Statistic_1()
    {...}
}

Here is "console.log(msg)" displays : Object {ErrorMessage: "Object reference not set to an instance of an object.", ErrorOccured: true, Items: null, NbRecord: 0} 这是“ console.log(msg)”显示:对象{ErrorMessage:“对象引用未设置为对象的实例。”,ErrorOccured:true,项目:null,NbRecord:0}

ResponseSatistic_1 class : 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;
    }
}

And Statistic_1 class : 和Statistic_1类:

public class Statistic_1
{
    private string _geografisch_zone;
    private decimal[] _sum; 
    private int _yearStart;
    private int _yearEnd;

    ...
}

您将返回字符串"OKKKKKK"但已在$.ajax函数中将dataType设置为JSON,因此必须取消设置dataType或返回诸如"{response: 'OKKKK'}"

Please change response = msg.Items; 请更改response = msg.Items; to response = msg; response = msg;

also the @campsjos answer was said that. 还有@campsjos的回答。

I solved the problem. 我解决了问题。

I had 2 projects in my solution, the WCF and an application console that will do to turn back the web service. 我的解决方案中有2个项目,即WCF和一个应用程序控制台,可以用来调回Web服务。

I also added the connectionstrings in app.config of the GettingStartedHost project and now it works well. 我还在GettingStartedHost项目的app.config中添加了连接字符串,现在它可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM