为什么AJAX响应未从PHP返回

发布于 2025-02-08 18:23:00 字数 6280 浏览 2 评论 0原文

我正在尝试通过Ajax取得PHP线程的进度,但是奇怪的是我现在面对的是, startpool() 在AJAX响应中可以看到方法,但在此过程中却不能看到,您会向我解释为什么会发生这种情况吗?

ajaxindex.php:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <meta name="description" content="Source code generated using layoutit.com">
    <meta name="author" content="LayoutIt!">

    
    
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link href="css/gijgo.min.css" rel="stylesheet" type="text/css" />
    <style>
        .error {
            border: 5px solid red !important;
        }
        
        .download-link {
            color: #26c605;
        }
        
        .download-error {
            color: #f20404;
        }
        
    </style>
  </head>
  <body>

    <div class="container-fluid">
    <div class="row">
        <div class="col-md-4">
            <form id="county-form" role="form" autocomplete="off">
                <div class="form-group">
                     
                    <label for="courtHouse">
                        Choose an action to scrap:
                    </label>
                    <select id="courtHouse" name="get-data" class="form-control">
                        <option value="none"></option>
                        <option value="all" selected="selected">Scrap.</option>
                        <option value="force">Force scrap.</option>
                    </select>
                </div>
                
                <button id="scrape-button" type="button" class="btn btn-primary">
                    Scrap now
                </button>
                <p id="result"></p>
            </form>
        </div>
    </div>
</div>

    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/gijgo.min.js" type="text/javascript"></script>
    <script src="js/scripts.js"></script>
    
    <script>
        
        $(document).ready(function() {
            let result_label = $("p#result");

            $("button#scrape-button").on('click', function(e){
                e.preventDefault();
                
                result_label.removeClass('download-error').html('');
                
                if($('#courtHouse option:selected').text() === '') {
                    $("#courtHouse").css("border", "2px solid red");
                    $("#courtHouse").focus();
                    return;
                }
                
                $("#courtHouse").css("border", "0.916667px solid rgba(0, 0, 0, 0.15)");
                $("#courtHouse").focus();
                
                result_label.html('<b>Please don\'t close the browser until the link appears here.</b>');
                const formData2 = $("#county-form").serialize();
                let last_response = '';
                $.ajax({
                    //url: "testAJAX.php",
                    url: "AjaxThread.php",
                    method: 'POST',
                    data: formData2,
                    dataType: 'text',
                    //contentType: 'application/json',
                    encode: true,
                    xhrFields: {
                        onprogress: function(e) {
                            let response = e.target.response.replace(/\n/g, '').split("|").slice(0, -1).pop();
                            //console.log('response split: ', e.target.response.replace(/\n/g, '').split("|").slice(0, -1));
                            console.log('response pop: ', response);
                            //console.log('response pop: ', JSON.parse(response).replace(/\n/g, ''));
                        }
                    },
                    success: function (result, status, xhr) {
                        console.log('success step');
                        console.log(result.status);
                        if(result.status === 'success') {
                            result_label.html(result.data);
                        }
                        else {
                            result_label.removeClass('download-link').addClass('download-error').html(result.data);
                        }
                    },
                    error: function(request, status, error) {
                        result_label.removeClass('download-link').addClass('download-error').html('Internal error, please contact the support.');
                        console.log('error step');
                        //console.log(JSON.stringify(request));
                        console.log(request);
                        console.log(status);
                        console.log(error);
                    }
                });
                
                
            });
        });
    </script>
  </body>
</html>

ajaxthread.php:

<?php

    require_once __DIR__ . '/vendor/autoload.php';


    use \ByJG\PHPThread\ThreadPool as ScrapperPool;

    try {
        $scrapper_pool = new ScrapperPool();
        for($i = 0; $i < 10; ++$i) {
                $rfc_scrapper = new ScrapperThread($i);
                $scrap = function(ScrapperThread $instance) { 
                    return $instance->scrap(); 
                };
                $scrapper_pool->queueWorker($scrap, [$rfc_scrapper]);
        }

        $scrapper_pool->startPool();
        $scrapper_pool->waitWorkers();

        // Get results
        file_put_contents('AjaxThreadCount', count($scrapper_pool->getThreads())); //
        foreach($scrapper_pool->getThreads() as $worker) {
            echo($scrapper_pool->getThreadResult($worker));
        }
    }
    catch(Exception $e) {
        echo $e->getMessage();
    }
?>

I am trying to get the PHP threads progress via AJAX, but the strange think I am facing right now is that any code before startPool() method can be seen in the AJAX response but not after it, would you explain to me why this is happening?

AjaxIndex.php:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <meta name="description" content="Source code generated using layoutit.com">
    <meta name="author" content="LayoutIt!">

    
    
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link href="css/gijgo.min.css" rel="stylesheet" type="text/css" />
    <style>
        .error {
            border: 5px solid red !important;
        }
        
        .download-link {
            color: #26c605;
        }
        
        .download-error {
            color: #f20404;
        }
        
    </style>
  </head>
  <body>

    <div class="container-fluid">
    <div class="row">
        <div class="col-md-4">
            <form id="county-form" role="form" autocomplete="off">
                <div class="form-group">
                     
                    <label for="courtHouse">
                        Choose an action to scrap:
                    </label>
                    <select id="courtHouse" name="get-data" class="form-control">
                        <option value="none"></option>
                        <option value="all" selected="selected">Scrap.</option>
                        <option value="force">Force scrap.</option>
                    </select>
                </div>
                
                <button id="scrape-button" type="button" class="btn btn-primary">
                    Scrap now
                </button>
                <p id="result"></p>
            </form>
        </div>
    </div>
</div>

    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/gijgo.min.js" type="text/javascript"></script>
    <script src="js/scripts.js"></script>
    
    <script>
        
        $(document).ready(function() {
            let result_label = $("p#result");

            $("button#scrape-button").on('click', function(e){
                e.preventDefault();
                
                result_label.removeClass('download-error').html('');
                
                if($('#courtHouse option:selected').text() === '') {
                    $("#courtHouse").css("border", "2px solid red");
                    $("#courtHouse").focus();
                    return;
                }
                
                $("#courtHouse").css("border", "0.916667px solid rgba(0, 0, 0, 0.15)");
                $("#courtHouse").focus();
                
                result_label.html('<b>Please don\'t close the browser until the link appears here.</b>');
                const formData2 = $("#county-form").serialize();
                let last_response = '';
                $.ajax({
                    //url: "testAJAX.php",
                    url: "AjaxThread.php",
                    method: 'POST',
                    data: formData2,
                    dataType: 'text',
                    //contentType: 'application/json',
                    encode: true,
                    xhrFields: {
                        onprogress: function(e) {
                            let response = e.target.response.replace(/\n/g, '').split("|").slice(0, -1).pop();
                            //console.log('response split: ', e.target.response.replace(/\n/g, '').split("|").slice(0, -1));
                            console.log('response pop: ', response);
                            //console.log('response pop: ', JSON.parse(response).replace(/\n/g, ''));
                        }
                    },
                    success: function (result, status, xhr) {
                        console.log('success step');
                        console.log(result.status);
                        if(result.status === 'success') {
                            result_label.html(result.data);
                        }
                        else {
                            result_label.removeClass('download-link').addClass('download-error').html(result.data);
                        }
                    },
                    error: function(request, status, error) {
                        result_label.removeClass('download-link').addClass('download-error').html('Internal error, please contact the support.');
                        console.log('error step');
                        //console.log(JSON.stringify(request));
                        console.log(request);
                        console.log(status);
                        console.log(error);
                    }
                });
                
                
            });
        });
    </script>
  </body>
</html>

AjaxThread.php:

<?php

    require_once __DIR__ . '/vendor/autoload.php';


    use \ByJG\PHPThread\ThreadPool as ScrapperPool;

    try {
        $scrapper_pool = new ScrapperPool();
        for($i = 0; $i < 10; ++$i) {
                $rfc_scrapper = new ScrapperThread($i);
                $scrap = function(ScrapperThread $instance) { 
                    return $instance->scrap(); 
                };
                $scrapper_pool->queueWorker($scrap, [$rfc_scrapper]);
        }

        $scrapper_pool->startPool();
        $scrapper_pool->waitWorkers();

        // Get results
        file_put_contents('AjaxThreadCount', count($scrapper_pool->getThreads())); //
        foreach($scrapper_pool->getThreads() as $worker) {
            echo($scrapper_pool->getThreadResult($worker));
        }
    }
    catch(Exception $e) {
        echo $e->getMessage();
    }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没有你 2025-02-15 18:23:00

检查这是否有帮助

<?php

    require_once __DIR__ . '/vendor/autoload.php';


    use \ByJG\PHPThread\ThreadPool as ScrapperPool;

    try {
        $scrapper_pool = new ScrapperPool();
        for($i = 0; $i < 10; ++$i) {
                $rfc_scrapper = new ScrapperThread($i);
                $scrap = function(ScrapperThread $instance) { 
                    return $instance->scrap(); 
                };
                $scrapper_pool->queueWorker($scrap, [$rfc_scrapper]);
        }

        $scrapper_pool->startPool();
        $scrapper_pool->waitWorkers();
        $result_getThreads = $scrapper_pool->getThreads();
        // Get results
        file_put_contents('AjaxThreadCount', count($result_getThreads)); //
        if($result_getThreads){
            foreach($result_getThreads as $worker) {
                echo($scrapper_pool->getThreadResult($worker));
            }
        }else{
            echo 'Something went wrong';
        }
        
    }
    catch(Exception $e) {
        echo $e->getMessage();
    }
?>

Check if this helps

<?php

    require_once __DIR__ . '/vendor/autoload.php';


    use \ByJG\PHPThread\ThreadPool as ScrapperPool;

    try {
        $scrapper_pool = new ScrapperPool();
        for($i = 0; $i < 10; ++$i) {
                $rfc_scrapper = new ScrapperThread($i);
                $scrap = function(ScrapperThread $instance) { 
                    return $instance->scrap(); 
                };
                $scrapper_pool->queueWorker($scrap, [$rfc_scrapper]);
        }

        $scrapper_pool->startPool();
        $scrapper_pool->waitWorkers();
        $result_getThreads = $scrapper_pool->getThreads();
        // Get results
        file_put_contents('AjaxThreadCount', count($result_getThreads)); //
        if($result_getThreads){
            foreach($result_getThreads as $worker) {
                echo($scrapper_pool->getThreadResult($worker));
            }
        }else{
            echo 'Something went wrong';
        }
        
    }
    catch(Exception $e) {
        echo $e->getMessage();
    }
?>
栩栩如生 2025-02-15 18:23:00

重要的Apache Weberver信息

尝试通过命令行运行ajaxthread.php,并查看forkhandler.php)是否与(exit(exit())一起使用。因为您执行AJAX请求,所以我想您通过Apache Web服务器运行。在这种情况下,exit()不起作用,您应该使用:

posix_kill(getmypid(), SIGKILL);

Important Apache webserver information

Try to run AjaxThread.php through command line and see if it works with (exit() in ForkHandler.php). Because you do an ajax request I suppose you run this through an apache webserver. In this case the exit() does not work, you should use instead:

posix_kill(getmypid(), SIGKILL);
允世 2025-02-15 18:23:00

此行:

for($i = 0; $i < 10; ++$i) {

更改

for($i = 0; $i < 10; $i++) {

Change this line:

for($i = 0; $i < 10; ++$i) {

to

for($i = 0; $i < 10; $i++) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文