为了账号安全,请及时绑定邮箱和手机立即绑定

完成之前响应 jquery ajax

完成之前响应 jquery ajax

PHP
拉丁的传说 2023-08-26 10:21:05
我想在响应完成之前显示我在 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>
查看完整描述

2 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

为了显示进度,您可以设置检查进程状态的时间间隔。如下所示:


  <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>


查看完整回答
反对 回复 2023-08-26
?
呼如林

TA贡献1798条经验 获得超3个赞

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


<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>


查看完整回答
反对 回复 2023-08-26
  • 2 回答
  • 0 关注
  • 86 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信