Post 如何发布到iframe?

Post 如何发布到iframe?,post,iframe,Post,Iframe,如何将数据发布到iframe?取决于“发布数据”的含义。您可以在标记上使用HTMLtarget=”“属性,因此它可以非常简单: <form action="do_stuff.aspx" method="post" target="my_iframe"> <input type="submit" value="Do Stuff!"> </form> <!-- when the form is submitted, the server respons

如何将数据发布到iframe?

取决于“发布数据”的含义。您可以在
标记上使用HTML
target=”“
属性,因此它可以非常简单:

<form action="do_stuff.aspx" method="post" target="my_iframe">
  <input type="submit" value="Do Stuff!">
</form>

<!-- when the form is submitted, the server response will appear in this iframe -->
<iframe name="my_iframe" src="not_submitted_yet.aspx"></iframe>

如果不是这样,或者你想要的是更复杂的东西,请编辑你的问题以包含更多细节

Internet Explorer中有一个已知的bug,只有在使用Javascript动态创建iFrame等时才会出现(有一个),但如果使用普通HTML标记,就可以了。目标属性和帧名称不是什么聪明的忍者黑客;虽然它在HTML4-Strict或XHTML1-Strict中被弃用(因此不会验证),但它自3.2以来一直是HTML的一部分,正式成为HTML5的一部分,并且自Netscape 3以来几乎在所有浏览器中都能工作

我已经在使用XHTML 1 Strict、XHTML 1 Transitional、HTML 4 Strict和“怪癖模式”时验证了这种行为,并且没有指定DOCTYPE,并且在所有情况下都可以使用Internet Explorer 7.0.5730.13。我的测试用例由两个文件组成,在IIS6上使用经典ASP;它们在这里被完整复制,所以你可以自己验证这种行为

default.asp

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Form Iframe Demo</title>
  </head>
  <body>
  <form action="do_stuff.asp" method="post" target="my_frame">
    <input type="text" name="someText" value="Some Text">
    <input type="submit">
  </form>
  <iframe name="my_frame" src="do_stuff.asp">
  </iframe>
  </body>
</html>
<%@Language="JScript"%><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Form Iframe Demo</title>
  </head>
  <body>
  <% if (Request.Form.Count) { %>
  You typed: <%=Request.Form("someText").Item%>
  <% } else { %>
  (not submitted)
  <% } %>
  </body>
</html>

表单Iframe演示
do_stuff.asp

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Form Iframe Demo</title>
  </head>
  <body>
  <form action="do_stuff.asp" method="post" target="my_frame">
    <input type="text" name="someText" value="Some Text">
    <input type="submit">
  </form>
  <iframe name="my_frame" src="do_stuff.asp">
  </iframe>
  </body>
</html>
<%@Language="JScript"%><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Form Iframe Demo</title>
  </head>
  <body>
  <% if (Request.Form.Count) { %>
  You typed: <%=Request.Form("someText").Item%>
  <% } else { %>
  (not submitted)
  <% } %>
  </body>
</html>

表单Iframe演示
您键入:
(未提交)

我非常希望听到任何浏览器不能正确运行这些示例。

iframe用于在html页面中嵌入另一个文档

如果表单要提交到表单页面中的iframe,那么可以使用标记的target属性轻松实现

将表单的target属性设置为iframe标记的名称

<form action="action" method="post" target="output_frame">
    <!-- input elements here --> 
</form>
<iframe name="output_frame" src="" id="output_frame" width="XX" height="YY">
</iframe>           


高级iframe目标使用
此属性还可用于产生类似ajax的体验,尤其是在文件上载等情况下,在这种情况下,必须提交表单才能上载文件

可以将iframe的宽度和高度设置为0,并且可以在将目标设置为iframe的情况下提交表单,并在提交表单之前打开加载对话框。因此,它模拟ajax控件,因为该控件仍然保留在输入表单jsp上,并且加载对话框处于打开状态

埃克斯马普

<script>
$( "#uploadDialog" ).dialog({ autoOpen: false, modal: true, closeOnEscape: false,                 
            open: function(event, ui) { jQuery('.ui-dialog-titlebar-close').hide(); } });

function startUpload()
{            
    $("#uploadDialog").dialog("open");
}

function stopUpload()
{            
    $("#uploadDialog").dialog("close");
}
</script>

<div id="uploadDialog" title="Please Wait!!!">
            <center>
            <img src="/imagePath/loading.gif" width="100" height="100"/>
            <br/>
            Loading Details...
            </center>
 </div>

<FORM  ENCTYPE="multipart/form-data" ACTION="Action" METHOD="POST" target="upload_target" onsubmit="startUpload()"> 
<!-- input file elements here--> 
</FORM>

<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;" onload="stopUpload()">   
        </iframe>

$(“#uploadDialog”).dialog({autoOpen:false,modal:true,closeOnEscape:false,
打开:函数(事件,ui){jQuery('.ui对话框标题栏关闭').hide();});
函数startupboad()
{            
$(“上传对话框”).dialog(“打开”);
}
函数stopUpload()
{            
$(“上传对话框”).dialog(“关闭”);
}

正在加载详细信息。。。
此函数创建一个临时表单,然后使用jQuery发送数据:

function postToIframe(data,url,target){
    $('body').append('<form action="'+url+'" method="post" target="'+target+'" id="postToIframe"></form>');
    $.each(data,function(n,v){
        $('#postToIframe').append('<input type="hidden" name="'+n+'" value="'+v+'" />');
    });
    $('#postToIframe').submit().remove();
}

如果您想更改iframe中的输入,那么从该iframe提交表单,请执行以下操作

...
var el = document.getElementById('targetFrame');

var doc, frame_win = getIframeWindow(el); // getIframeWindow is defined below

if (frame_win) {
  doc = (window.contentDocument || window.document);
}

if (doc) {
  doc.forms[0].someInputName.value = someValue;
  ...
  doc.forms[0].submit();
}
...
通常,您只能在iframe中的页面来自同一来源时执行此操作,但您可以在调试模式下启动Chrome,忽略同一来源策略,并在任何页面上进行测试

function getIframeWindow(iframe_object) {
  var doc;

  if (iframe_object.contentWindow) {
    return iframe_object.contentWindow;
  }

  if (iframe_object.window) {
    return iframe_object.window;
  } 

  if (!doc && iframe_object.contentDocument) {
    doc = iframe_object.contentDocument;
  } 

  if (!doc && iframe_object.document) {
    doc = iframe_object.document;
  }

  if (doc && doc.defaultView) {
   return doc.defaultView;
  }

  if (doc && doc.parentWindow) {
    return doc.parentWindow;
  }

  return undefined;
}

正如下面所指出的,表单上的目标可能在IE7中不起作用,您需要对此进行测试。IE7的问题是,如果您使用javascript生成iframe,则不会设置名称标记(即使您尝试设置),为什么post目标会失败。IE7的解决方案:是否有办法将输出帧的内容保存到文件中(例如,表单提交到的服务器的响应)?@ZeroGraviti不太清楚“保存到文件”是什么意思。您可以做的是将表单提交到IFRAME,并将响应设置为中所述的内容处置标头-因此用户单击“提交”,它将发布到IFRAME,浏览器接收响应并提示用户将文件保存到其本地计算机。这就是你想要的吗?@DylanBeattie,让我澄清一下我的用例。我已经能够填充一个iframe,它被设置为html
target
属性。在表单发布后,我可以在目标iframe中看到内容,我想为用户提供一个选项,将此内容保存到文件中。这就是我想问的。如果这需要更清楚的说明,请告诉我。谢谢,正如您所写,这很重要