dtoqemais553654797 2013-12-03 13:59
浏览 92
已采纳

如何使用Jquery将数据发布到iframe

How can I dynamically post data to an iframe in Jquery.

I really need to post data to an Iframe in this case, I cannot use a $.POST because data received is returned sequentially (buffered)

If you have a workaround to make jquery handle data returned by a $.POST 'when it receives the data. I'm very curious!

At the moment I handle it with GETS this way:

var iframe = $('<iframe style="display:none;"></iframe>');
$( "body" ).append(iframe);
iframe.attr('src','server.php?type=getFolders&inode='+nodeData.inode).load(function(){$(this).remove()});

This basically creates a temporary iframe and lets the php inject javascript in it (using ob_flush();flush(); )while it is returning data, then when it's finished, it simply removes the iframe to clean up.

from within the iframe, I access the main frame with window.parent. then the mainframe's methods.

this is ideal but works with GET, how can I make this work with POST ?

  • 写回答

3条回答 默认 最新

  • doujiang1993 2013-12-03 15:12
    关注

    Ok, so as this apparently doesn't exist, I created my own solution. Sharing it here in case anybody wants to POST to an iFrame in jQuery.

    the js function/ class-like:

    function iframeform(url)
    {
        var object = this;
        object.time = new Date().getTime();
        object.form = $('<form action="'+url+'" target="iframe'+object.time+'" method="post" style="display:none;" id="form'+object.time+'" name="form'+object.time+'"></form>');
    
        object.addParameter = function(parameter,value)
        {
            $("<input type='hidden' />")
             .attr("name", parameter)
             .attr("value", value)
             .appendTo(object.form);
        }
    
        object.send = function()
        {
            var iframe = $('<iframe data-time="'+object.time+'" style="display:none;" id="iframe'+object.time+'"></iframe>');
            $( "body" ).append(iframe); 
            $( "body" ).append(object.form);
            object.form.submit();
            iframe.load(function(){  $('#form'+$(this).data('time')).remove();  $(this).remove();   });
        }
    }
    

    then when you need to send a form to a temporary iframe :

    var dummy = new iframeform('server.php');
    dummy.addParameter('type','test');
    dummy.addParameter('message','Works...');
    dummy.send();
    

    This is the server.php example file :

    if($_POST[type] == 'test') 
    {
        header( 'Content-type: text/html; charset=utf-8' );
        echo '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>';
        echo str_pad('',4096); //fill browser buffer
    
        for($i = 0; $i < 10; $i++)
            {
                echo '<script type="text/javascript">window.parent.console.log(\''.$_POST[message].'\');</script>';
                ob_flush(); flush();
                usleep(350000);
            }
    }
    

    And the result is as expected:

    the main frame's console outputs the string 'Works...' every 350ms starting immediately, even if the php is still running.

    When the php is finished sending the chunks, it simply removes the temporary form and the temporary iframe.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题