Passing 2 Callbacks Gives Undefined As Result
Q1: The callbacks both work, but I get 4 results instead of 2. I get onderhoud+undefined aswell as macro+undefined. How do I solve this? Q2: how do I pass all the variables from lo
Solution 1:
This is how it works
var onderhoud;
var macro;
function loadOnderhoud(getData) {
var username = window.sessionStorage.getItem("huidigeGebruiker");
var url = "restservices/gebruiker?Q1=" + username;
$.ajax({
url : url,
method : "GET",
beforeSend : function(xhr) {
var token = window.sessionStorage.getItem("sessionToken");
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
},
success : function(data) {
var onderhoud;
$(data).each(function (index) {
if (this.geslacht == "m"){
onderhoud = (66 + (13.7 * this.gewicht) + (5 * (this.lengte*100)) - (6.8 * this.leeftijd)) * this.activiteit;
}
else if (this.geslacht == "v"){
onderhoud = (655 + (9.6 * this.gewicht) + (1.8 * (this.lengte*100)) - (4.7 * this.leeftijd)) * this.activiteit;
}
});
getData(onderhoud);
},
});
}
// Load ingredients from JSON test file
function loadMacro(getData) {
var username = window.sessionStorage.getItem("huidigeGebruiker");
var datum = document.getElementById("datepicker").value;
var url = "restservices/ingredients?Q1=" + username + "&Q2=" + datum;
$.ajax({
url : url,
method : "GET",
async: false,
beforeSend : function(xhr) {
var token = window.sessionStorage.getItem("sessionToken");
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
},
success : function(data) {
var totalCal=0;
var totalVet=0;
var totalVv=0;
var totalEiwit=0;
var totalKh=0;
var totalVezels=0;
var totalZout=0;
$(data).each(function (index) {
totalCal = (this.hoeveelheid * this.calorieen) / 100;
totalVet = (this.hoeveelheid * this.vet) / 100;
totalVv = (this.hoeveelheid * this.verzadigd_vet) / 100;
totalEiwit = (this.hoeveelheid * this.eiwit) / 100;
totalKh = (this.hoeveelheid * this.koolhydraten) / 100;
totalVezels = (this.hoeveelheid * this.vezels) / 100;
totalZout = (this.hoeveelheid * this.zout) / 100;
});
getData(totalCal);
},
});
}
function getData(data)
{
onderhoud = data;
}
function getData2(data)
{
macro = data;
}
loadOnderhoud(getData);
loadMacro(getData2);
Post a Comment for "Passing 2 Callbacks Gives Undefined As Result"