2017-06-15 65 views
0

Q1:回調都可以,但是我得到了4個結果,而不是2個。我得到了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); 

回答

0

這是它如何工作的

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); 
+0

如果我在1個功能需要兩個變量,是聲明變量的全局和的getData內設置他們我最好的選擇?或者,還有更好的方法?另外,你能告訴我如何從loadMacro()獲得每個函數中的所有變量嗎?非常感謝你! –

+0

在這種情況下,您需要創建一個ajax調用鏈,一個接一個ajax調用不是異步的 –

+0

這樣做我仍然無法使用getData函數之外的數據。重點是將所有信息彙總在一個功能中。我試圖在getData函數外部聲明該變量並將其設置在函數內部,但這使我再次未定義。 –

相關問題