简体   繁体   English

处理多个Ajax请求

[英]Handling multiple Ajax requests

I'm developing an application which requires multiple ajax requests, such that Option2 will be only available when Button2 will be clicked and its response returned. 我正在开发一个需要多个ajax请求的应用程序,这样Option2只有在点击Button2并返回其响应时才可用。 Similarly, Option3 will be available only when Button3 has been clicked. 同样,只有在单击Button3时, Option3才可用。 Following is the general structure of the code. 以下是代码的一般结构。 I'm building the app in php & mysql . 我在phpmysql构建应用程序。

$("#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");
        }
    });
});

Currently, everything works. 目前,一切正常。 The application will be for a maximum of 10,000 users. 该应用程序最多可容纳10,000名用户。 I just wanted to know if there was any way better of doing this/or any frameworks available that I can incorporate. 我只是想知道是否有更好的方法来实现这个/或任何我可以合并的框架。

Also: What may be the problems that may arise using this way and ways to clear out those problems. 另外:使用这种方式可能出现的问题以及清除这些问题的方法。

There is a cleaner way of doing it, with jQuery. 使用jQuery有一种更简洁的方法。 Using the .ready function. 使用.ready函数。

$("#button1").click(function() {
    $.ajax({
        type: "POST",
        url:  "url-1",
        data: { id: //some-id },
        success: function(response) {
            $('.menu-right').html(response);
        },
        error: function(error) {
            alert("Error");
        }
    });
});

//When I'm ready... provide a click event
$(".button2").ready(function(){
    $(".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);
            },
            error: function(error) {
                alert("Error");
            }
        });
    });
});
$(".button2").ready(function(){
    $('.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");
            }
        });
    });
});

The problems that arrive doing it your way. 以你的方式做到的问题。 Due too much nesting (For instance, if you need to add 4 more buttons) it could lead too.. technical debt which is much harder to maintain. 由于nesting太多(例如,如果你需要增加4个按钮),它也可能导致技术债务,这是很难维护的。

Avoid creating deeply nested if-then statements since they are harder to read and error-prone to maintain. 避免创建深层嵌套的if-then语句,因为它们更难以阅读且易于维护。 source 资源

You could go further with this, and better encapsulate each ajax request into a singular function and write your own jQuery callback . 你可以更进一步,更好地将每个ajax请求封装到一个单一函数中并编写自己的jQuery callback

you can also use jquery's .on functionality to not have to nest those 你也可以使用jquery的.on功能来嵌套那些

$('body').on('click' ,'.button2', doButton2request);
$('body').on('click' ,'.button3', doButton3request);

$("#button1").click(doButton1request)

function doButton1request()
{
    do ajax and on success put in the new button
}

function doButton2request()
{
   do ajax and on success put in the new button
}

function doButton3request()
{
   do ajax and on success do "some things to do"
}

the on function wires up the event so that whenever anybody clicks on anything (within 'body'), if the class is .button2 for example, it calls the function with that (this) being the item matched. on函数连接事件,以便每当有人点击任何内容时(在'body'内),如果该类是.button2,则它调用该函数(this)作为匹配的项。 you you can add and remove the button or the .button2 class as needed. 您可以根据需要添加和删除按钮或.button2类。 also with .off you can temporarily stop those events from firing. 也可以使用.off,你可以暂时停止这些事件的发射。

So the general idea is, whenever you have clickable items that won't exist at the beginning of the app, you can set up an .on event. 因此,一般的想法是,只要您有可在应用开始时不存在的可点击项目,您就可以设置.on事件。 Or, you can have items that don't receive clicks until they have another class added, like ".active" or something. 或者,在添加了另一个类之前,您可以拥有不会获得点击的项目,例如“.active”或其他类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM