Javascript 传递2个回调将给出未定义的结果
问题1:回调都有效,但我得到4个结果,而不是2个。我得到onderhood+未定义以及macro+未定义 我如何解决这个问题 问题2:如何在回调中传递loadMacro中的所有变量,并在回调中单独使用它们Javascript 传递2个回调将给出未定义的结果,javascript,ajax,Javascript,Ajax,问题1:回调都有效,但我得到4个结果,而不是2个。我得到onderhood+未定义以及macro+未定义 我如何解决这个问题 问题2:如何在回调中传递loadMacro中的所有变量,并在回调中单独使用它们 function loadOnderhoud(getData) { var username = window.sessionStorage.getItem("huidigeGebruiker"); var url = "restservices/gebruiker?Q1=
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, dataa)
{
var onderhoud = data;
var macro = dataa;
console.log(onderhoud, macro);
}
loadOnderhoud(getData);
loadMacro(getData);
这就是它的工作原理
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);
如果我想在一个函数中同时使用两个变量,那么我最好的选择是将变量声明为全局变量并在getData中设置它们吗?还是有更好的办法?另外,您能告诉我如何从loadMacro()获取每个函数中的所有变量吗?非常感谢你!在这种情况下,您需要创建一个ajax调用链,一个接一个的ajax调用不是异步的。这样做,我仍然无法使用getData函数之外的数据。整个要点是将所有信息集中在一个函数中。我试图在getData函数外部声明变量并在函数内部设置它,但这会再次导致未定义。为此,必须在函数外部定义这些变量