javascript 如何每 20 秒将 $.ajax (jquery) 响应加载到 iframe?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14865658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:46:49  来源:igfitidea点击:

How to load $.ajax (jquery) response to iframe every 20 seconds?

javascriptjqueryiframe

提问by user1788736

How i can load this jquery get response to middle iframe in my page? The php script loads some data from mysql db? in this case i am not passing any data to php so do i need to make get or post request ? my code display the jquery response in alert but doesn't load it into iframe!

我如何加载这个 jquery 来响应我页面中的中间 iframe?php 脚本从 mysql db 加载一些数据?在这种情况下,我没有将任何数据传递给 php,所以我需要发出 get 或 post 请求吗?我的代码在警报中显示 jquery 响应,但没有将其加载到 iframe 中!

    setInterval(function() {

      $.ajax(
    {
        type: 'GET',
        url: './getDataFromMySQLdb.php',
        //data: $("#myform").serialize(),

         data: {
         title: 'test',
          // wrapper: 'testing'
         },
                success: function (good)
                {
                  //handle success

                      alert(good);
                    $("#middle").attr('src',+good); //change url
                },
                failure: function (bad)
                {
                   //handle any errors

                  alert(bad)

                }


    });
    }, 20000);

<iframe name="top" src="blank.php" id="top"  scrolling="no" noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="79"></iframe>
<iframe NAME="middle" src="blank.php" id="middle" noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="238" scrolling="auto"></iframe>
<iframe NAME="foot" src="blank.php" id="foot"  scrolling="no"  noresize frameborder="0" marginwidth="0" marginheight="0" width="100%" height="200"></iframe>

回答by Dangelov

You can simply reload directly the iframe every 20 seconds, no need for $.ajax() :

您可以每 20 秒直接重新加载 iframe,不需要 $.ajax() :

setInterval(function(){
   $("#middle").attr('src', './getDataFromMySQLdb.php');
}, 20000);

However, I think that it will be better for you to not use an iframe at all. Instead, just use a div with the same id='middle', and load the result directly into it:

但是,我认为根本不使用 iframe 对您来说会更好。相反,只需使用具有相同 id='middle' 的 div,并将结果直接加载到其中:

setInterval(function(){
   $("#middle").load('./getDataFromMySQLdb.php');
}, 20000);

回答by dev

There seems to be some errors in your AJAX script, but you would be right in using a GET request. Please see below for something that might be what you're looking for. Also I can't see why you are loading this information into an iFrame? I would load it into a div instead. I might be completely misunderstood in what you're trying to achieve so the below might be of no help.

您的 AJAX 脚本中似乎存在一些错误,但您使用 GET 请求是正确的。请参阅下面的内容,了解可能是您正在寻找的内容。另外我不明白您为什么要将此信息加载到 iFrame 中?我会把它加载到一个 div 中。我可能完全误解了您要实现的目标,因此以下内容可能没有帮助。

setInterval(function() {
      $.ajax({
        type: 'GET',
        url: '../getDataFromMySQLdb.php',
        data: '',
        success: function (data){
           $("#middleDiv").html(data);
        },
        failure: function (){
           alert('Error message');
        }
    });
}, 20000);

<div id="middleDiv"></div>

<div id="middleDiv"></div>