Return .done s string in a Jquery Ajax response

0

I have the following code:

function GET_URLAsync(strUrl, aParm) {

    var response = $.ajax({
        type: "POST",
        url: "../Paginas/" + strUrl,
        data: JSON.stringify(aParm),
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
    });
    response.always(function (data) {
        //replace to complete
    });
    response.done(function (data) {//replace to success
        var registros = data.d;
        if ($.type(registros) != "null") {
            //dataReturn = registros;
            return registros
        } else {
            console.log("Data NULL del servidor ");
            return [];
        }
    });
    response.then(function (data) {
        //Incorporates the functionality of the .done() and .fail() methods
    });
    response.fail(function (data) { //replace to error
        // handle request failures
        swal("Error petición GET");
    });
    return response;
}

It is a standard function that makes calls to the server. the way I use it is as follows.

    //Llamada al servidor de manera Asincrona. :D
    GET_URLAsync(strUrl, aParm).done(function (periodos) {
        console.log(periodos)
        f_LlenarListaPeriodosAlumnos(periodos);
    });

The problem is that doing console.log(periodos) . I get an object, and to recover my list I have to put again the .d ( periodos.d ).

How could I make the .done previous of the standard function return the clean list to me and no longer have to put .d . Something like:

$.ajax({})
.done(function(){
return "hola "
})
.done(function(dataHola){
return dataHola+" Vitmar"
})
.done(function (dataHolaVitmar){
console.log(dataHolaVitmar)
});

Will it be possible? Thanks.

    
asked by Vitmar Aliaga 19.07.2017 в 22:26
source

1 answer

1

The done function always receives the object received in the AJAX response as an argument.

But you can use a then before the done to filter this value, staying only with the .d property if it's what you need:

GET_URLAsync(strUrl, aParm)
    .then(function(periodos) {
        return periodos.d;
    })
    .done(function (d) {

    });
    
answered by 19.07.2017 / 23:32
source