简体   繁体   中英

Function success in Ajax Jquery isn't executed

I have this code in Jquery and it displays always error function:

function getStatistic6() {

var response;
var allstat6 = [];
var dstart = "01.01.2014";
var dend = "03.31.2014";
$.ajax({
    type: 'GET',
    url: 'http://localhost:52251/Service1.asmx/Statistic_6_Entete',
    data: { "start": dstart, "end": dend },
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    dataType: 'json',
    success: function (msg) {
        response = msg.d;
        for (var i = 0; i < response.Items.length; i++) {
            var j = 0;
            allstat6[i] = [response.Items[i].Date, response.Items[i].Piece, response.Items[i].Tiers, response.Items[i].AmoutHT, response.Items[i].AmountTTC, response.Items[i].Quantite];
        }
        fillDataTable6(allstat6);
        $('table').visualize({ type: 'line' });

    },
    error: function (e) {
        alert("error loading statistic 6");
    }
});
}

When I check the debug, my web service method returns the good xml response. When I display the status and error with alert, I have :

在此处输入图片说明

What's wrong?

EDIT :

I have this in my webservice.asmx :

[WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public ResponseStatistic_6_Entete Statistic_6_Entete(DateTime start, DateTime end)
    {
        ...
    }

在此处输入图片说明

Sorry I misspoke, I want to return json objects. I use other methods whose datatype is json and it works very well. When I say that it returns the correct xml response, it is because I saw it in the debug that's all. I do not understand why with this method one, it does not work!

my web service method returns the good xml response.

but

dataType: 'json',

You've told your code to ignore what the server says the data is and to try to parse it as JSON. Since it is not JSON but is XML, it fails.

Remove the dataType property.


Then you have:

 for (var i = 0; i < response.Items.length; i++) { 

but you will need to replace that with DOM methods (or jQuery wrappers around them) to access the data since it will be an XML DOM and not a simple JS object.

As you mentioned in question, the web service response in xml then please use dataType:"xml" in ajax function

As well as use $.parseXML() for parse XML response data.

I solved my problem with JSON.stringify:

function getStatistic6() {

var response;
var allstat6 = [];
var dstart = "01.01.2014";
var dend = "03.31.2014";
$.ajax({
    type: 'GET',
    url: 'http://localhost:52251/Service1.asmx/Statistic_6_Entete',
    data: {"start": JSON.stringify(dstart), "end": JSON.stringify(dend) },
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (msg) {
        response = msg.d;
        for (var i = 0; i < response.Items.length; i++) {
            var j = 0;
            allstat6[i] = [response.Items[i].Date, response.Items[i].Piece, response.Items[i].Tiers, response.Items[i].AmoutHT, response.Items[i].AmountTTC, response.Items[i].Quantite];
        }
        fillDataTable6(allstat6);
        $('table').visualize({ type: 'line' });

    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("error loading statistic 6");
        alert("Status: " + textStatus+"\n"+"Error: " + errorThrown);
    }
});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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