【问题标题】:Response jquery ajax before done在完成之前响应jquery ajax
【发布时间】:2020-07-25 04:36:35
【问题描述】:

我想在响应完成之前显示我在 php 中刷新的信息。 我搜索并编写了以下代码 进度完成后我得到响应,但我想在 php 脚本进程和用户查看正在做什么时显示结果。 我该怎么做?

谢谢

我的代码:

<!doctype html>
<html lang="">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<form>
    <input type="text" id="ip" value="127.0.0.1">
    <button type="button" onclick="save()">send</button>
</form>

<iframe id="loadarea"></iframe>


<script src="jquery-3.5.1.min.js"></script>
<script type="text/javascript">
    function save() {
        let ip = $('#ip').val();
        let req = $.ajax({
            type: 'POST',
            url: 'xample.php',
            data: {
                ip: ip
            }
        });

       **************************************************************
       *                                                            *
       *  req.progress(function (res) {                             *
       *      document.getElementById('loadarea').src = 'nmap.php'; *
       *  });                                                       *
       *                                                            *
       **************************************************************
        req.done(function (res) {
            if (res == "cant") {
                 console.log('fail');
            }else
                 console.log('ok' );
        });
    }
</script>
</body>
</html>

【问题讨论】:

    标签: php jquery ajax response flush


    【解决方案1】:

    为了显示进度,您可以设置检查进程状态的时间间隔。像这样:

      <script type="text/javascript">
        function save() {
          let ip = $('#ip').val();
          let req = $.ajax({
            type: 'POST',
            url: 'xample.php',
            data: {
              ip: ip
            }
          });
    
          req.done(function (res) {
    
    
    //================================
            //  set interval for check status of progress every 1 sec
            setInterval(() => {
              let reqStatus = $.ajax({
                type: 'POST',
                url: 'xampleStatus.php',
                data: {
                  ip: ip
                }
              });
              req.done(function (resStatus) {
                console.log(resStatus);
              });
    
            }, 1000);
    
    
    //================================
    
            if (res == "cant") {
              console.log('fail');
            } else
              console.log('Start progress');
          });
    
    
    
        }
      </script>
    

    【讨论】:

    • 感谢 foadabd 我认为 setInterval 是为了串联做事,但我想在完成之前显示结果一次。
    【解决方案2】:

    我用这种方式解决了这个问题:

    <script src="jquery.min.js"></script>
    <script type="text/javascript">
        function save() {
            var ip = $('#ip').val();
            var data = new FormData();
            data.append('ip', ip);
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'example.php', true);
    
            xhr.send(data);
            xhr.onreadystatechange = function () {
                if (xhr.status === 200) {
                    if (xhr.readyState === XMLHttpRequest.LOADING) {
                        $('#loadarea').html(xhr.response);
                    }
                    if (xhr.readyState === XMLHttpRequest.DONE) {
                        $('#loadarea').append("finished!!!");
                    }
                }
            }
        }
    </script>
    

    【讨论】:

      猜你喜欢
      • 2017-03-15
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2020-02-16
      • 2017-09-30
      • 1970-01-01
      • 2017-10-24
      • 2011-10-04
      相关资源
      最近更新 更多