php - 为什么对 Symfony Controller 的 jQuery AJAX 请求是并行处理的而不是异步的?

标签 php jquery ajax symfony asynchronous

当使用 jQuery $.ajax({...}) 将简单数据发布到普通 PHP 脚本时,多个请求在平行线。当使用 Symfony 2.8 Controller 作为目标时,请求是同步处理的。 这是为什么?

纯 HTML 和 PHP 设置

// Plain PHP file: /testscript.php
<?php 
    sleep($_POST['time']);
    echo $_POST['id'];


// Plain HTML file: /testpage.html
<html>
<head>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
Click here:
<div id='testbtn' style="background-color: #abc">Click</div>

 <script>
    $(document).ready(function() {
        var start = new Date().getTime();
        var count = 1;

        $("#testbtn").click(function() {
            var time = new Date().getTime();
            console.log('Click at '+count+':' + (time - start));

            $.ajax({
                url : '/testscript.php',
                type : "post",
                data : {'time':3, 'id':count},
                async: true,
                context : this,
                success : function(data) {
                    var end = new Date().getTime();
                    console.log('Click Success: ' + data + "  -  " + (end - start));
                }
            });

            count++;
        });


        $.ajax({
            url : '/testscript.php',
            type : "post",
            data : {'time':10, 'id':0},
            async: true,
            context : this,
            success : function(data) {
                var end = new Date().getTime();
                console.log('Auto Success: ' + data + "  -  " + (end - start));
            }
        });

        console.log('Ajax fired');
    });
</script>

</body>
</html>    

Symfony 设置

// Controller Action to handle /sym_testscript.php
public function testScriptAction(Request $request) {
    sleep($request->get('time'));
    return new Response($request->get('id'), Response::HTTP_OK);
}


// Controller Action to handle /sym_testpage.php
public function testPageAction() {
    return $this->render('MyBundle::sym_testpage.html.twig');
}   


// Twig-Template for /sym_testpage.html
...exactly the same HTML code as above. Twig is only used to insert URL
...
$.ajax({
    url : '{{ path('sym_testscript') }}',
    ...

页面 /testpage.html 在加载 10 秒的 sleep 值时调用 /testscript.php。当点击 Button 几次时,页面加载瀑布看起来像这样:

1: ========================================   // initial call of testscript.php
2:     ============                           // testscript.php called by 1 click
3:      ============                          // testscript.php called by 2 click
4:         ============                       // testscript.php called by 3 click

每次单击按钮都会立即调用 testscript.php,然后与初始调用和其他按钮调用并行执行。所以每个点击调用运行 3 秒。

当使用 Symfony 版本时,瀑布看起来像这样:

1: ========================================   // initial call of testscript.php
2:     ====================================================                           
3:      ================================================================                          
4:         ============================================================================

同样,每次单击按钮都会立即调用 /sym_testscript.php。但是现在电话一个接一个地处理。因此,总运行时间不是 10 秒,而是 19 = 10 + 3 + 3 + 3...

当在纯 HTML 文件中使用 sym_testscript.php 作为目标时,结果是一样的。因此问题似乎出在 Symfony Controller 中...

这是为什么? 为什么在使用 Symfony 解决方案时没有并行处理 ajax 调用?

最佳答案

一旦您在 php 中启动 session ,php 就会锁定它,后续请求将不得不等到 session 再次可用。

因此,如果您的 symfony 脚本使用 session ,那么当 session 打开时,您一次只能使用该 session 执行 1 个请求。

禁用 session (如果这甚至是一个选项...)或在您不再需要它时将其关闭将允许并行请求。

关于php - 为什么对 Symfony Controller 的 jQuery AJAX 请求是并行处理的而不是异步的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52038243/

相关文章:

php - 为什么我在使用 enctype ="multipart/form-data"时会得到 undefined index

php - WooCommerce - 为免费用户计划添加运费

php - 调度方法

jquery - $ 美元符号

javascript - 将项目添加到列表顶部并向下滚动

jQuery AJAX Post 不发布数据

javascript - 通过数组 Angular 绑定(bind) ajax 数据

php - php print_r 语句末尾的 "1"是什么意思?

javascript - jQuery UI 可拖动,捕捉到网格

jquery - 使用 AJAX、Spring MVC、MySQL 更新表