0
Q1:コールバックはどちらも機能しますが、2ではなく4つの結果が得られます。私はonderhoud + undefined、さらにマクロ+2コールバックを渡すと結果は未定義になります
どうすれば解決できますか?
Q2:コールバックで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);
をどのように動作するかである私は1つの機能の両方の変数をしたい場合は、変数はグローバル宣言したgetData内でそれらを設定するための私の最高のオプションですか?それとも良い方法がありますか?また、loadMacro()から各関数内のすべての変数を取得する方法を教えてください。どうもありがとうございました! –
この場合、ajax呼び出しのチェーンを作成し、別のajax呼び出しを非同期的に作成する必要はありません –
これを行うにはまだgetData関数以外のデータは使用できません。全体のポイントは、すべての情報を1つの機能でまとめていることです。私はgetData関数の外で変数を宣言しようとし、関数内でそれを設定しましたが、これは私に再び未定義を与えます。 –