This was working fine but has for some reason stopped, I'm fetching a JSON string from an mvc controller action, which according to console.log is returning fine, however actually trying to use the array of objects returns undefined. This has nothing do to with Ajax being asynchronous as the returned data is there it's just not usable.
The ajax call & chart function:
$(document).ready(function () {
$.ajax({
url: '/Home/GetSessionGraphData',
type: 'POST',
success: function (result) {
console.log(result);
$.each(result.GraphData, function (stat) {
addData(myChart, stat.Time, stat.Stat)
});
},
error: function () {
}
});
function addData(chart, label, data) {
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
dataset.data.push(data);
});
chart.update();
}
The controller function:
public JsonResult GetSessionGraphData()
{
return Json(new { _api.GraphData }, JsonRequestBehavior.AllowGet);
}
My console.log
inside the success:
{GraphData: Array(2)}
GraphData: Array(2)
0: {Time: "9:35", Stat: 0}
1: {Time: "9:40", Stat: 1001}
However trying to actually use that data is returning undefined.