我正在开发一个需要多个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