7

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;
    }
4
  • It is unclear what you are showing in the console, is it 'data' or 'jqXHR'. I suspect it's the latter. You want to use the data parameter to your success function. Commented Mar 8, 2017 at 17:07
  • success and error function cant return anything because you return the the ajax function ;) modify it by remove return before $.ajax and try it again. and normaly in success function data sould contain jqXHR.responseText Commented Mar 8, 2017 at 17:12
  • @mtizziani I tried to remove the return from the ajax function but I still need to return the color hexa string from this method anyway :) .. May I ask you if you can write a simple example for that ? Commented Mar 9, 2017 at 3:51
  • @MikeS Actually I didn't write anything in either success or error function .. all I need is to receive the color hexa string from the method. Commented Mar 9, 2017 at 3:57

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes I did and I exactly got the color hex code .. but I need to pass this value to another JS method for coloring the chart parts !
For the first solution I tried to do what you wrote but I didn't receive any results and for the second one I received Uncaught ReferenceError: data is not defined in the console window.
Thank you Mateus , your solution helps me too much through my way to find the solution :).
Can you give a check in my answer? ;)
1

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 :).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.