0
I’m trying to accomplish the loop on the return of this request AJAX. In the first event success
, I make a second call AJAX which will return - in this case - 3 objects JSON.
My doubt is how to create, in the second event success
, 3 dataPoints that will generate and popular 3 graphics made in canvas?
Code jQuery:
$.ajax({
type: 'get',
url: 'dadosPS.php?ps=getGerencialPergunta&pesquisa='+slct_ps+'&datainicio='+dtin+'&datafim='+dtf,
dataType: 'json',
async: true,
beforeSend: function () {
},
success: function (data, textStatus, jqXHR) {
var dataPoints = [];
var dataPoints2 = [];
var tt = 0;
var count = $.map(data.data, function(n, i) { return i; }).length;//conta qtd de perguntas
$.each(data.data, function (key, val) {
id = val.id;
dataPoints.push({y:parseInt(val.qtd),label:val.pergunta});
if(val.nr_pergunta == '1'){
tt = val.qtd++;
}
$.ajax({
type: 'get',
url: 'dadosPS.php?ps=getGerencialResposta&id='+id+'&pesquisa='+slct_ps+'&datainicio='+dtin+'&datafim='+dtf,
dataType: 'json',
async: false,
beforeSend: function () {
},
success: function (data, textStatus, jqXHR) {
console.log(data);
},
complete: function () {
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
});
//grafico das perguntas
var chart = new CanvasJS.Chart("teste0", {
//colorSet: "customColorSet1",
title: {
text: tt,
//horizontalAlign: "left"
verticalAlign: "center",
dockInsidePlotArea: true
},
animationEnabled: true,
exportEnabled: true,
legend: {
maxWidth: 350,
itemWidth: 120
},
data: [{
type: "doughnut",
indexLabel: "{label} {y}",
//startAngle: 240,
//showInLegend: true,
//legendText: "{label}",
dataPoints: dataPoints
}]
});
chart.render();
},
complete: function () {
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
Return of the second success
explain a little better please, you already make the creation of datapoints in the first call, why not replicate the strategy in the second call?
– h3nr1ke
Then in the first I give a given and the code has an id I read in each and pass that id to the second ajax.... in this case are three ids and on the return of the second ajax I need to create the 3 Graphics but I am not getting.
– Carlos Lopes
I haven’t gone over the details, but just seeing an Ajax being called into a loop each gives me the creeps. Imagine calling one Ajax after another without doing "queue"... I don’t know if this works.
– Sam
Of course it’s not the best way but if you have another way, please give me a hint :)
– Carlos Lopes