2017-03-08 65 views
1

我有一个DoughnutChart图表,我想改变其颜色的数据库中保存的颜色十六进制代码我使用这种Ajax方法通过调用一个操作方法来获取颜色字符串返回JSON结果,从Ajax方法返回字符串结果

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

,而不是接受我接收到的对象的readyState {1}的字符串,但在控制台窗口enter image description here

不过,我可以找到存储在responseText的element.I颜色值需要你帮助我如何获得字符串的颜色值。

编辑:

为了让事情更清楚这就是我想调用的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; 
    } 
+0

目前还不清楚你在控制台上显示的是'data'还是'jqXHR'。我怀疑这是后者。你想使用数据参数到你的成功功能。 – MikeS

+0

成功和错误函数不能返回任何东西,因为你返回ajax函数;)通过删除$ .ajax之前的返回来修改它并再次尝试。并且正常成功的函数数据应该包含jqXHR。responseText – mtizziani

+0

@mtizziani我试图从ajax函数中移除返回值,但我仍然需要从该方法返回颜色hexa字符串:)无论如何,我可以问你是否可以为此写一个简单的例子? – FreedomDeveloper

回答

1

为什么你的代码是这样的?

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

你使用它了吗?

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

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

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

及使用您的函数使用此代码:

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

或者你可以使用一个回调

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

...以及与回调使用:

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

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

+0

是的,我做了,我完全得到了颜色十六进制代码..但我需要将此值传递给另一个JS方法为图表部件着色! – FreedomDeveloper

+0

对于第一个解决方案,我试着去做你写的东西,但是我没有收到任何结果,第二个解决方案收到Uncaught ReferenceError:数据未在控制台窗口中定义。 – FreedomDeveloper

+1

谢谢Mateus,你的解决方案通过我的方式来帮助我找到解决方案:)。 – FreedomDeveloper

1

解决方案

所有我要感谢马特乌斯Koppe他的努力:首先,通过他的解决方案我解决我的问题的方式.. 我所做的仅仅只是我收到这个responseText从进入成功的结果在我的Ajax方法,然后我把它传给一个回调函数,处理类似如下的结果:

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

然后我用getColorArrayModified通过我的类别列表的工作循环和填充自己的颜色。

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

谢谢大家:)。