Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
Returning String Result from Ajax Method

I have a DoughnutChart chart and I would like to change the color of its parts regarding color hexa-codes saved in the database I used this Ajax method to get the color string by invoking an action method that returns JSON Result ,

    getcolors: function getcolors(name) {
    return $.ajax({
        url: "/api/ideas/getcolors",
        data: { name: name },
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data, textStatus, jqXHR) {
           // return  data;
        },
        error: function (data) {
          // return  "Failed";
        },
        async: true
    });

but instead of receiving the string I received Object {readyState: 1} in the console window enter image description here

However, I can find the color value stored in ResponseText element.I need your help in how can I get the color value as string.

EDIT :

To make things more clear that's where I would like to invoke the ajax method to receive the color string then I will be able to push in the chart color array .

getColorArray: function getColorArray(categories) {
        var colors = [];
        for (var i = 0; i < categories.length; i++) {
            console.log(this.getcolors("Risk"));
            //colors.push(this.getcolors(categories[i]));
        }

        return colors;
    }
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Why your code is like this?

success: function (data, textStatus, jqXHR) { 
   // return  data;
},

Did you use it?

success: function (data, textStatus, jqXHR) {
   console.log(data);
}

Ok, i got it. When you use an ajax request your will work with asynchronous data, to do this you need return a promise in your method. Please, try to use the code below.

getcolors: function getcolors(name) {
    return $.ajax({
       url: "/api/ideas/getcolors",
       data: { name: name },
       type: "GET",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
    });
}

And for use your function use this code:

getcolors("name").done(function(result){
    console.log(result);
});

Or you can use a callback

getcolors: function getcolors(name, success, error) {
    return $.ajax({
       url: "/api/ideas/getcolors",
       data: { name: name },
       type: "GET",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function(data){
           success(data);
       },
       error: function(data){
           error(data);
       }
    });
}

... And for use with callbacks:

getcolors("name", function(data){
    //success function
    console.log(data);
}, function(){
    //Error function
    console.log(data);
})

Try one of this options and tell the result.

about 4 years ago · Santiago Trujillo Relatório

0

The Solution

First of all I would like to thank Mateus Koppe for his efforts, through his solution I got the way to solve my problem .. What I did simply is just I received the ResponseText from the incoming successful result in my Ajax method and then I passed it to a callback function that handles the result like the following :

getcolors: function getcolors(name, handleData) {
$.ajax({
    url: "/api/ideas/getcolors",
    data: { name: name },
    type: "GET",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        handleData(data.responseText);
        //return data.responseText;
    },
    error: function (data) {
        handleData(data.responseText);
        //return data.responseText;
    },
    async: false
});

then I worked with getColorArrayModified to loop through my categories list and populate its own color.

    getColorArrayModified: function getColorArrayModified(categories) {
    var colors = [];
    for (var i = 0; i < categories.length; i++) {
        this.getcolors(categories[i], function (output) {
            colors.push(output);
        });
    }
    return colors;
}

Thanks for all :).

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda