Ahm*_*att 5 javascript ajax asp.net-mvc jquery asp.net-core-mvc
我有一个 DoughnutChart 图表,我想根据保存在数据库中的颜色六进制代码更改其部分的颜色,我使用此 Ajax 方法通过调用返回 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
});
Run Code Online (Sandbox Code Playgroud)
但我没有收到字符串,而是在控制台窗口中收到了 Object {readyState: 1}
但是,我可以找到存储在 ResponseText 元素中的颜色值。我需要您帮助我如何将颜色值作为字符串获取。
编辑 :
为了让事情更清楚,我想调用 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;
}
Run Code Online (Sandbox Code Playgroud)
为什么你的代码是这样的?
success: function (data, textStatus, jqXHR) {
// return data;
},
Run Code Online (Sandbox Code Playgroud)
你用过吗?
success: function (data, textStatus, jqXHR) {
console.log(data);
}
Run Code Online (Sandbox Code Playgroud)
好,我知道了。当您使用 ajax 请求时,您将使用异步数据,为此您需要在方法中返回一个承诺。请尝试使用下面的代码。
getcolors: function getcolors(name) {
return $.ajax({
url: "/api/ideas/getcolors",
data: { name: name },
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
});
}
Run Code Online (Sandbox Code Playgroud)
为了使用你的函数,请使用以下代码:
getcolors("name").done(function(result){
console.log(result);
});
Run Code Online (Sandbox Code Playgroud)
或者您可以使用回调
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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
...并与回调一起使用:
getcolors("name", function(data){
//success function
console.log(data);
}, function(){
//Error function
console.log(data);
})
Run Code Online (Sandbox Code Playgroud)
尝试此选项之一并告诉结果。
归档时间: |
|
查看次数: |
20282 次 |
最近记录: |