![](/img/trans.png)
[英]Passing PHP array using json_encode method into external Javascript function gives result as 'undefined'
[英]Passing 2 callbacks gives undefined as result
Q1:回調都可以,但是我得到4個結果而不是2個。我得到onderhoud + undefined以及macro + undefined。
我該如何解決?
問題2:如何在回調函數中傳遞來自loadMacro的所有變量,並分別在回調函數中使用它們?
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.