Ajax和PHP-为什么代码可以在本地服务器上运行,但不能在托管服务器上运行?

时间:2019-12-06 15:14:29

标签: php jquery ajax asynchronous synchronous

我应该怎么做才能在主机上进行Ajax工作?我尝试制作进度条。我想在迭代完成后立即显示"echo <p>$i</p>"。在本地服务器上,"echo <p>$i</p>"工作并在迭代完成时立即显示$ i。在托管<?php if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { $i = 0; echo "<p>$i</p>"; flush(); ob_flush(); while ($i < 5) { $i++; echo "<p>$i</p>"; flush(); ob_flush(); sleep(1); } exit(); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Output PHP Script Progress via Ajax - Basic Example</title> <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css"> </head> <body> <div class="container"> <h2>Output PHP Script Progress via Ajax - Basic Example</h2> <p>Cick button below to run script.</p> <form class="" role="form" action="/basic.php" method="post"> <button type="submit" class="btn btn-primary">Execute Long PHP Script</button> </form> <div id="progress"></div> </div> <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script type="text/javascript" src="./dist/script-basic.min.js"></script> </body> </html> 时有效,并且仅在脚本执行后才显示$ i。代码可以在本地服务器上运行,但不能在主机上运行。我认为在托管或请求标头上设置php时遇到麻烦。我试图找到解决此问题的方法,但没有找到。请帮帮我!

PHP和HTML代码:

$(document).ready(function() {
    $("form").submit(function(e) {
        $.ajax({
            url: $(this).attr("action"),
            xhrFields: {
                onprogress: function(e) {
                    console.log(e.target.responseText), $("#progress").html(e.target.responseText)
                }
            },
            success: function(e) {
                console.log(e), $("#progress").html(e + "<h1>done!</h1>")
            }
        }), e.preventDefault()
    })
});

带有Ajax的JS代码:

git checkout -b prX #branch for pr X
# this is one way I squash
git reset --soft HEAD~4 # set branch pointer 4 revisions back... all changes for featureX are in index
git commit -m "Feature X"
# push this branch so that the PR is created
# then, when the branch is merged to master:
git checkout featureX # original development branch for the feature
git merge -m "merging changes from master" master

0 个答案:

没有答案
相关问题