1
是否可以同時執行$ .post請求?我有以下代碼:
function loadTest() {
var questionIDs = [];
var count = 0;
console.log("getting test");
$.post("db.php", function(data) {
obj = jQuery.parseJSON(data);
var questionCount = obj.length;
console.log(obj);
while (count != questionCount) {
questionIDs.push(obj[count].id);
count++;
}
console.log(questionIDs);
// $('#question').html(obj[0].question);
});
$.post("getChoices.php", {
IDs: questionIDs
}, function(data) {
console.log(data);
obj = jQuery.parseJSON(data);
console.log(obj);
while (q < 3) {
console.log("question");
var w = 1;
while (w < 5) {
console.log(obj[q][w]);
w++;
}
q++;
}
// $('#question').html(obj[0].question);
});
}
由於asycnchronously運行$。員額功能,在發送時可變questionIDs將是空的。有沒有可能的解決方案,不需要從$ .post更改爲ajax?我在這裏嘗試了各種解決方案,但我似乎無法讓他們工作。
在函數中調用第二個調用,並在您有權訪問'questionIDs'後,從第一個閉包內調用它。你也可以在'.done'部分放置第二個函數。 – Axalix
你可以這樣做。 'var res =''; (函數(響應1){函數(響應){res =響應;})。然後($。post()。done(函數(響應1){alert(res +「」+ response1}));' –