我有這個代碼從2個URL使用$.getJSON()
檢索JSON,並將結果保存爲變量。然後我使用$.when()
和then()
從這些變量中獲取數據。但是,這隻適用於我一次只做一個,每個都有自己的$.when()
,並且在我同時使用時不起作用。JQuery.when多個JSON變量
var player = $.getJSON("http://api.hivemc.com/v1/player/" + $user + "/timv");
var game = $.getJSON("http://api.hivemc.com/v1/game/timv");
$.when(player,game).then(function(maindata, data){
$('#1').text(maindata.total_points);
$('#2').text(maindata.i_points);
$('#3').text(maindata.t_points);
$('#4').text(maindata.d_points);
$('#5').text(maindata.role_points);
$('#6').text(maindata.most_points);
if(maindata.detectivebook == true)
$('#7').text("Yes");
else
$('#7').text("No");
$flare = maindata.active_flareupgrade;
$flare = $flare.charAt(0).toUpperCase() + $flare.slice(1).toLowerCase();
$('#8').text($flare);
$('#9').text(maindata.title);
var d = new Date(maindata.lastlogin * 1000);
var n = d.toISOString();
$('#10').text(d.getDate() + "/" + d.getMonth() + "/" + d.getFullYear());
});
控制檯的錯誤,我得到的是:
jquery-latest.min.js:2 Uncaught TypeError: Cannot read property 'length' of undefined at Function.each (jquery-latest.min.js:2) at Object. (dr:112) at Function.each (jquery-latest.min.js:2) at Object. (dr:108) at Object. (jquery-latest.min.js:2) at j (jquery-latest.min.js:2) at Object.fireWith [as resolveWith] (jquery-latest.min.js:2) at x (jquery-latest.min.js:4) at XMLHttpRequest.b (jquery-latest.min.js:4)`
誰能告訴我什麼,我做錯了什麼?謝謝。
你收到控制檯任何錯誤? – 31piy
嗯,我使用000Webhost在開發過程中託管我的網站,我不確定如何查看控制檯 –
在瀏覽器中按F12,或者右鍵單擊某個元素並單擊'檢查' –