从Ajax方法返回字符串结果

时间:2021-11-07 10:49:21

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 ,

我有一个DoughnutChart图表我想改变它部分的颜色关于在数据库中保存的颜色,我使用Ajax方法通过调用返回JSON结果的操作方法来获取颜色字符串,

    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 从Ajax方法返回字符串结果

但是,我没有接收字符串,而是在控制台窗口中接收对象{readyState: 1}

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.

但是,我可以找到存储在ResponseText元素中的颜色值。我需要你的帮助,我怎样才能得到颜色值为字符串。

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 .

为了更清楚地说明这一点,我希望在这里调用ajax方法来接收颜色字符串,然后我将能够推入图表颜色数组。

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;
    }

2 个解决方案

#1


1  

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.

好的,我明白了。当您使用ajax请求时,您将处理异步数据,为此需要在方法中返回一个承诺。请尝试使用下面的代码。

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.

尝试其中一个选项并告诉结果。

#2


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 :

首先我要感谢Mateus Koppe的努力,通过他的解决,我找到了解决我的问题的方法。我所做的仅仅是在Ajax方法中从传入的成功结果中接收到ResponseText,然后将其传递给一个回调函数,该函数处理如下所示的结果:

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来循环我的category列表并填充它自己的颜色。

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

感谢所有:)。

#1


1  

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.

好的,我明白了。当您使用ajax请求时,您将处理异步数据,为此需要在方法中返回一个承诺。请尝试使用下面的代码。

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.

尝试其中一个选项并告诉结果。

#2


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 :

首先我要感谢Mateus Koppe的努力,通过他的解决,我找到了解决我的问题的方法。我所做的仅仅是在Ajax方法中从传入的成功结果中接收到ResponseText,然后将其传递给一个回调函数,该函数处理如下所示的结果:

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来循环我的category列表并填充它自己的颜色。

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

感谢所有:)。