2017-03-08 73 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; 
} 

謝謝大家:)。