猿问

完成之前响应 jquery ajax

我想在响应完成之前显示我在 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>


拉丁的传说
浏览 120回答 2
2回答

慕妹3242003

为了显示进度,您可以设置检查进程状态的时间间隔。如下所示:&nbsp; <script type="text/javascript">&nbsp; &nbsp; function save() {&nbsp; &nbsp; &nbsp; let ip = $('#ip').val();&nbsp; &nbsp; &nbsp; let req = $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; url: 'xample.php',&nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ip: ip&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; req.done(function (res) {//================================&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; set interval for check status of progress every 1 sec&nbsp; &nbsp; &nbsp; &nbsp; setInterval(() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let reqStatus = $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'xampleStatus.php',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ip: ip&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; req.done(function (resStatus) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(resStatus);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }, 1000);//================================&nbsp; &nbsp; &nbsp; &nbsp; if (res == "cant") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('fail');&nbsp; &nbsp; &nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('Start progress');&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; </script>

呼如林

我用这种方式解决了这个问题:<script src="jquery.min.js"></script><script type="text/javascript">&nbsp; &nbsp; function save() {&nbsp; &nbsp; &nbsp; &nbsp; var ip = $('#ip').val();&nbsp; &nbsp; &nbsp; &nbsp; var data = new FormData();&nbsp; &nbsp; &nbsp; &nbsp; data.append('ip', ip);&nbsp; &nbsp; &nbsp; &nbsp; var xhr = new XMLHttpRequest();&nbsp; &nbsp; &nbsp; &nbsp; xhr.open('POST', 'example.php', true);&nbsp; &nbsp; &nbsp; &nbsp; xhr.send(data);&nbsp; &nbsp; &nbsp; &nbsp; xhr.onreadystatechange = function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (xhr.status === 200) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (xhr.readyState === XMLHttpRequest.LOADING) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#loadarea').html(xhr.response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (xhr.readyState === XMLHttpRequest.DONE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#loadarea').append("finished!!!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }</script>
随时随地看视频慕课网APP
我要回答