Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从url获取html并在div中显示_Javascript_Html - Fatal编程技术网

Javascript 如何从url获取html并在div中显示

Javascript 如何从url获取html并在div中显示,javascript,html,Javascript,Html,我想获取URL的主体并将其显示在DIV中。我无法获得我想要的结果 这是我的密码。 index.js html 出于安全原因,您不能使用fetch将请求发送到其他域 如果您确实需要这样做,您可以尝试使用jQuery和iFrames(阅读更多信息)。我可以从服务器获取并发送到客户端吗?因为当我使用console.log(body)时,它会显示github的HTML正文。我只需要将其附加到div以正确显示 fetch('https://github.com/') .then(res =>

我想获取URL的主体并将其显示在DIV中。我无法获得我想要的结果

这是我的密码。 index.js

html


出于安全原因,您不能使用fetch将请求发送到其他域


如果您确实需要这样做,您可以尝试使用jQuery和iFrames(阅读更多信息)。

我可以从服务器获取并发送到客户端吗?因为当我使用console.log(body)时,它会显示github的HTML正文。我只需要将其附加到div以正确显示
fetch('https://github.com/')
    .then(res => res.text())
    .then(body => document.getElementById('embed-html').innerHTML);
<div id="embed-html"></div>