Asynchronous request to PHP server without returning data? (Attached is the solution)

藏色散人
Release: 2023-04-10 21:34:02
forward
4848 people have browsed it

Recently discovered a ajax asynchronous request problem, use $.post, $.get, $.ajaxWhen requesting the PHP server, data cannot be returned asynchronously.

After many tests, we discovered:
– Different browsers, request different domain names - no blocking: no experiment required
– Different browsers, request the same domain name - Non-blocking: session_id() Returns different
– The same browser requests different domain names - Non-blocking: session_id Returns different
– The same browser requests the same domain name -Blocking: session_id() Returns the same

Found the problem:
1 Close XDEBUG
2 SESSION Lock
3 Clear the output buffer

1 CloseXDEBUG
XDEBUG is real-time debugging. When debugging, it will keep FPM to ensure the thread is working to avoid data pollution.
The typical test method is to open another browser and access the site when debugging using XDEBUG. The site is inaccessible at this time.

This has a significant impact on parallel responses, i.e. even if the frontend sends multiple requests, it is controlled by XDEBUG and can only respond to one at a time.
Also, since XDEBUG will still open automatically. 2 SESSIONLock

Use session_write_close() to close the write lock of SESSION, which is suitable for
SESSION is saved as File. Not required if SESSION is saved in Redis. 3 Clear the output bufferUsing

session_write_close()

may not close the SESSION lock immediately, so add it before this method :
ob_end_flush(). Let session_write_close() take effect immediately. 4 ExampleThere is an example below. When the [Submit] button is clicked, the front end will send two requests to the backend server.

One is the

get request, which is requested every 1 second. One is the
post
request, which is sent once at the beginning and then waits for the corresponding end. Look at the HTML code

<form>
<input type="submit" value="提交" />
</form>

<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>

<script type="text/javascript">
    $(&#39;form&#39;).on(&#39;submit&#39;, function(e) {
        e.preventDefault();

        // 每隔一秒请求一次服务器
        var id = setInterval(function() {
            $.get(
                &#39;save.php?action=get&#39;,
                {},
                function(data) {
                    console.log(data);
                },
                &#39;json&#39;
            );
        }, 1000);

        $.post(
            &#39;save.php?action=post&#39;,
            {},
            function(data) {
                console.log(data);
                // 停止定时循环
                clearInterval(id);
            },
            &#39;json&#39;
        );
    });
</script>
Copy after login
php code
<?php
session_start();$action = $_GET[&#39;action&#39;];
if ($action == &#39;post&#39;) {

   $_SESSION[&#39;time&#39;] = 0;
   session_write_close();

    while ($_SESSION[&#39;time&#39;] < 5) {
      session_start();
      $_SESSION[&#39;time&#39;] = $_SESSION[&#39;time&#39;] + 1;

      // 将SESSION数据写入文件中,并关闭写锁
      session_write_close();

      // sleep()模拟花费时间较长的程序,这样在关闭写锁之后,
      // 服务器就能够相应别的请求,如下的$action=get,
      sleep(1);
    }

    echo json_encode([session_id() => $_SESSION[&#39;time&#39;]]);
    exit();}if ($action == &#39;get&#39;) {
    echo json_encode([session_id() => $_SESSION[&#39;time&#39;]]);
    exit();}
Copy after login

Recommended study: "

PHP Video Tutorial

"                                                  ##

The above is the detailed content of Asynchronous request to PHP server without returning data? (Attached is the solution). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template