iFrame : How to display a server response (HTML) d

2019-06-04 07:21发布

问题:

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.

Thanks Gendaful

回答1:

With vanilla JavaScript:

document.MyFrame.document.body.innerHTML = serverResponse

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



回答2:

Not sure if this will work, I do something similar using jquery (but could do it just as easily without)...

$('iframe').contents().find("html")
    .append($("<head></head><body> blah blah blah </body>"));