简体   繁体   English

没有回调成功功能的嵌套AJAX请求

[英]Nested AJAX requests without a callback success function

After reading this thread jQuery Ajax Request inside Ajax Request 阅读此线程后, Ajax请求中的jQuery Ajax请求

Hi everyone I need to have explanations about such a situation. 大家好,我需要对这种情况进行解释。 I've just been working on the code of a former member of my development team and found many parts of the code where he makes asynchronous ajax calls within other ajax calls. 我刚刚从事开发团队前成员的代码工作,发现代码的许多部分在其他ajax调用中进行了异步ajax调用。

My question is : can anyone explain the advantages and disadvantages of this practice and whether it is a good or bad practice? 我的问题是 :谁能解释这种做法的优缺点,以及它是好是坏?

Here is an example of code: 这是代码示例:

// first ajax (starting ajax call)
$.ajax({
    url: "script1.php", 
    type: "POST", 
    data: {paramFisrtAjax: "first-ajax"},
    success: function(response) {
       alert(response);
    }
});

script1.php script1.php

<script>    
// second ajax
$.ajax({
    url: "script2.php", 
    type: "POST", 
    data: {paramFirstAjax: "<?= $_POST['paramFisrtAjax'] ?>", paramSecondAjax: "second-ajax"},
    success: function(response) {
        alert(response);
    }
});
</script>
<?php 
// some operations on database server
echo "page2 operations with param: paramFirstAjax-> {$_POST['paramFirstAjax']}"; 
?>

script2.php script2.php

<?php 
// some operations on database server
echo "page3 operations with params: firstParam -> {$_POST['paramFisrtAjax']} and secondParam-> {$_POST['paramSecondAjax']}";
?>

Something tells me it's not a good thing becouse i think the correct way is use the callback function success . 有人告诉我这不是一件好事,因为我认为正确的方法是使用success的回调函数。 Like this: jquery nested ajax calls formatting 像这样: jQuery嵌套的ajax调用格式

There is an advantage and a disadvantage here. 这里有一个优点和缺点。

The Advantages are: 优点是:

1) You make an async call, making the request a lot faster . 1) 进行异步调用,使请求更快 You do not wait for the callback function, thus do not wait for your response which might take time to return . 您无需等待回调函数, 因此不必等待可能需要一段时间才能返回的响应 You do everything on the background rather then 'straight forward'. 您在后台执行所有操作,而不是“直截了当”。 This is understandable when you call multiple methods and you do not want the delay in waiting for the callback. 当您调用多个方法并且您不希望延迟等待回调时,这是可以理解的。

2) You are able to fetch a far greater amount data through your call while minimizing the need of the end client to wait. 2) 您可以通过呼叫获取更多的数据,同时最大程度地减少最终客户端的等待时间。 This is useful when you have a big amount of data to display and you want to make it with minimal effort. 当您要显示大量数据并且想要以最小的努力来制作数据时,这很有用。

The Disadvantages: 缺点:

1) Error handling is a pain. 1)错误处理是一种痛苦。 I f something fails within the inner calls, it takes time to detect were the failure occurred and on which method . 如果内部调用中发生故障,则需要花费一些时间来检测故障发生的原因以及使用哪种方法 When waiting for the callback, you can detect right away where the error occurred, as it will return a response of success or error, 等待回调时,您可以立即检测到错误发生的位置,因为它将返回成功或错误的响应,

2) if there is a mismatch on the data, it is hard to track back and see where the missing part took place, you will have to go through each request one by one to detect and use developer tools and/or fiddler as well , since those are async calls at the end. 2)如果数据不匹配,则很难回溯并查看丢失部分的发生位置, 您将必须逐个处理每个请求以检测和使用开发人员工具和/或提琴手 ,因为这些是最后的异步调用。

3) it is easy to put too much effort on the client, since maintaining this kind of technique might result in calling multiple methods that will work together at the same time, thus creating overload on the client, locks on the threads or DB when working with server side code and more. 3)很容易在客户端上投入过多的精力,因为维护这种技术可能会导致调用多个可以同时工作的方法,从而在客户端上产生重载,在工作时锁定线程或DB与服务器端代码等。

This explained, you can now decide for yourself with which type of method you would like to continue further in your code. 这就说明了,您现在可以自己决定要在代码中进一步使用哪种类型的方法。

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

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