微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 为什么对Symfony控制器的jQuery AJAX请求是并行处理而不是异步处理?

使用jQuery $.ajax({…})将简单数据发布到普通PHP脚本时,会并行处理多个请求.当使用Symfony 2.8控制器作为目标时,同步处理请求.为什么是这样?

简单的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秒时调用/testscript.PHP.单击按钮几次页面加载瀑布看起来像这样:

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控制器内……

为什么是这样?
使用Symfony解决方案时,为什么不能并行处理ajax调用

解决方法:

一旦你在PHP中启动会话,PHP将锁定它,后续请求将不得不等到会话再次可用.

因此,如果您的symfony脚本使用会话,则在会话打开时,您只能使用该会话一次执行1个请求.

禁用会话(如果这是一个选项……)或在不再需要时关闭它将允许并行请求.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐