亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

完成之前響應 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 關注
  • 150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號