我正在開發一個需要多個Ajax請求的應用程序,這樣Option2
只有在Button2
將被點擊並返回響應時纔可用。同樣,只有當Button3
被點擊時,Option3
纔可用。以下是代碼的一般結構。我正在構建應用程序php
& mysql
。處理多個Ajax請求
$("#button1").click(function() {
$.ajax({
type: "POST",
url: "url-1",
data: { id: //some-id },
success: function(response) {
$('.menu-right').html(response);
$(".button2").click(function() { //button2 is created when the above response is printed as html
$.ajax({
type: "POST",
url: "url-2",
data: { id: this.id },
success: function(response) {
$('.menu-right-jump').html(response);
$('.button3').click(function() { ////button3 is created when the above response is printed as html
$.ajax({
type: "POST",
url: "url-3",
data: { id: this.id },
success: function(response) {
// some things to do
},
error: function(error) {
alert("Error");
}
});
});
},
error: function(error) {
alert("Error");
}
});
});
},
error: function(error) {
alert("Error");
}
});
});
目前,一切正常。該應用程序將最多爲10,000個用戶。我只是想知道是否有更好的方法來完成這個或者我可以使用的任何框架。
另外:使用這種方法和方法來清除這些問題可能會出現什麼問題。
這是非常醜陋的代碼...爲什麼你不能將這些Ajax請求封裝到單個函數中? –
@BradM:我只是一個初學者。我只是看免費教程,然後嘗試完成任務。你能不能舉一個封裝它們作爲單個函數的例子? – xan