Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!