本文介绍了iFrame:如何使用javascript将服务器响应(HTML)直接显示到iFrame中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个简单的服务器响应,它是一个html文件,我希望在不将文件保存到工作区或机器的情况下在iFrame中显示相同的内容。



我正在做一个ajax调用,如下所示。

  Ajax.request({
url:'url',
method:'POST',
success:function(response){

var responseHtmlStr = response.responseText ;

我在responseHtmlStr中获得的示例服务器响应如下。

 <!DOCTYPE html> 
< html>
< head>
< script>
函数copyText()
{
alert('It is clicked');
}
< / script>
< / head>
< body>

Field1:< input type =textid =field1value =Hello World!>< br>
Field2:< input type =textid =field2>
< br>< br>
< button onclick =copyText()>复制文本< / button>

< / body>
< / html>

我用来创建iFrame的代码如下。



;



由于我不想将服务器响应存储在服务器中的文件中。如何直接在iFrame中提供服务器响应?



我试过 document.getElementsByTagName('iframe')[0] .contentWindow.document.write(serverResponse); 但它不能与上面的代码一起工作。



请任何其他建议。感谢
Gendaful

解决方案

使用香草JavaScript:

  document.MyFrame.document.body.innerHTML = serverResponse 

..您有< iframe name =MyFrame>< / iframe>


I am getting a simple server response which is an html file and I want to display the same in iFrame without saving the file to my workspace or machine.

I am making an ajax call as below.

                   Ext.Ajax.request({
                        url : 'url',
                        method : 'POST',
                        success : function(response) {

                        var responseHtmlStr =response.responseText;

Sample Server response which I am getting in responseHtmlStr is as below.

<!DOCTYPE html>
<html>
<head>
<script>
function copyText()
{
alert('It is clicked');
}
</script>
</head>
<body>

Field1: <input type="text" id="field1" value="Hello World!"><br>
Field2: <input type="text" id="field2">
<br><br>
<button onclick="copyText()">Copy Text</button>

</body>
</html>

The code I am using to create the iFrame is as below.

;

As I dont want to store the server response in the server in a file. How to directly feed the server response in the iFrame?

I tried document.getElementsByTagName('iframe')[0].contentWindow.document.write(serverResponse);​ but it is not working with the above code.

Any other suggestions please. Thanks in advance.

ThanksGendaful

解决方案

With vanilla JavaScript:

document.MyFrame.document.body.innerHTML = serverResponse

..where you have <iframe name="MyFrame"></iframe>

这篇关于iFrame:如何使用javascript将服务器响应(HTML)直接显示到iFrame中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:46