Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
like image 325
Ahmed Elbatt Avatar asked Mar 08 '17 17:03

Ahmed Elbatt


People also ask

Can we return a value from ajax call?

ajax returns immediately and the next statement, return result; , is executed before the function you passed as success callback was even called.

What is used to get response of an ajax call as a string?

AJAX - Server Response The XMLHttpRequest object has an in-built XML parser. The responseXML property returns the server response as an XML DOM object.

What is returned by ajax?

ajax() function returns the XMLHttpRequest object that it creates. Normally jQuery handles the creation of this object internally, but a custom function for manufacturing one can be specified using the xhr option.

Does ajax return a promise?

ajax returns a promise object, so that we don't have to pass a callback function.


2 Answers

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.

like image 155
Mateus Koppe Avatar answered Sep 30 '22 03:09

Mateus Koppe


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

like image 23
Ahmed Elbatt Avatar answered Sep 30 '22 02:09

Ahmed Elbatt