1

I am trying to get some information from Database and make a chart with them. Unfortunately, the variables I use to store that values are losing their data outside of POST. completed and ongoing are assigned inside their POST methods, also each other's post methods, but not outside of it. When data2 executes, the variables become empty again. How can I fix this issue?

var completed;
var ongoing;

$.post(
    "/api/EducationCompletionRates?status=1&startDate=20150701&endDate=20150705",
    { data: {} },
    function (data) {
        completed = data;
    }
);

$.post(
    "/api/EducationCompletionRates?status=2&startDate=20150701&endDate=20150705",
    { data: {} },
    function (data) {
        ongoing = data;
    }
);

var data2 = [
    {
         value: completed,
         color: "#46BFBD",
         highlight: "#5AD3D1",
         label: "Completed"
    },
    {
         value: 700, //ongoing,
         color: "#FDB45C",
         highlight: "#FFC870",
         label: "Ongoing"
    }
]
kalahari
  • 895
  • 5
  • 15
  • 34

3 Answers3

1

This is due to the AJAX requests being asynchronous, not due to variable scope. For a full explanation of the issue, see the answers in this question. To solve the problem in your specific case, you can use the $.when calback to create your data2 object after both AJAX calls have completed. Try this:

var completed;
var ongoing;
var requests = [];

requests.push($.post(
    "/api/EducationCompletionRates?status=1&startDate=20150701&endDate=20150705",
    { data: {} },
    function (data) {
        completed = data;
    }
));

requests.push($.post(
    "/api/EducationCompletionRates?status=2&startDate=20150701&endDate=20150705",
    { data: {} },
    function (data) {
        ongoing = data;
    }
));

$.when.apply(this, requests).done(function() {
    var data2 = [{
         value: completed,
         color: "#46BFBD",
         highlight: "#5AD3D1",
         label: "Completed"
    },
    {
         value: ongoing,
         color: "#FDB45C",
         highlight: "#FFC870",
         label: "Ongoing"
    }];

    // add further logic which relies on data2 here...
});

Note that this approach is quicker than nesting AJAX calls as they will run in parallel, rather than in a chain. Therefore the user will only be waiting as long as the slowest request.

Community
  • 1
  • 1
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

$.post() is an AJAX call and it is supposed to be asynchronous so your POST calls did not have any effect on the array creation line because that was executed before AJAX were complete.

var data2 = [];

$.post(
    "/api/EducationCompletionRates?status=1&startDate=20150701&endDate=20150705",
    { data: {} },
    function (data) {
        data2[0] = {
            value: completed,
            color: "#46BFBD",
            highlight: "#5AD3D1",
            label: "Completed"
        }
    }
);

$.post(
    "/api/EducationCompletionRates?status=2&startDate=20150701&endDate=20150705",
    { data: {} },
    function (data) {
        data2[1] = {
            value: 700, //ongoing,
            color: "#FDB45C",
            highlight: "#FFC870",
            label: "Ongoing"
        }
    }
);

Or you can do only one ajax call because the second one cause the same output all the time.

rrk
  • 15,677
  • 4
  • 29
  • 45
0

this fix the problem:

before the ajax define your data2 as array

inside the first function do

function (data) {
    data2.push({
         value: data,
         color: "#46BFBD",
         highlight: "#5AD3D1",
         label: "Completed"
    });
}

and this for the second

function (data) {
    data2.push({
         value: data,
         color: "#FDB45C",
         highlight: "#FFC870",
         label: "Ongoing"
    });
}
Vanojx1
  • 5,574
  • 2
  • 23
  • 37