简体   繁体   中英

Inject HTML response into an iframe (from an AJAX call)

I won't beat around the bush: my AJAX call returns a message object response (message = {...}), where the messages.description parameter is an HTML string which looks something like this:

"<!doctype html>
<html>
  <head>
    <style>
      ...some style
    </style>
  </head>
  <body>
    ...some content
  </body>
</html>"

is there any way to embed it into an iframe? I'm not really familiar with iframes and any help on the matter is appreciated!

Also, I'm using vanilla React without babel or webpack, so React.createElement() aplenty. This is just to give new features an outline since my company wants to shift into React + node.js in the following months

You can inject your html to the iframe as below,

HTML:

<iframe id="iFrameID" src="javascript:void(0);"></iframe>

Javascript:

document.getElementById('iFrameID').src = "data:text/html;charset=utf-8," + escape(htmlContent);

You can't, because it's prohibited in policy of browsers crossdomain.

But you can in one case if you have control over the domain you want to update html in the iframe.

more information https://spring.io/understanding/CORS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM