【问题标题】:How to pull code from another domain如何从另一个域中提取代码
【发布时间】:2012-08-13 19:40:29
【问题描述】:

如果我有几个网站,并且我想在每个网站上阅读“file.html”,我应该怎么做? file.html 应该是这样的:

<h1>Hot news</h1>
<p>article</p>

我知道我可以使用 php include 或 require,

<? include 'file.html'; ?>

或 jQuery,但仅限于域内。

.load("file.html");

我应该如何跨域?

PS:是的,我知道这是不安全的

【问题讨论】:

  • 不能跨域,除非远程服务器支持 JSONP 请求,例如除非该站点与您合作,否则您不能使用 javascript 窃取其他站点的 html。不过,您可以尝试使用 iframe 做一些事情。

标签: php jquery include cross-domain


【解决方案1】:

你可以使用 PHP:

<?php echo file_get_contents('http://....'); ?>

include 不起作用的原因是默认情况下禁用 url 包含,因为它们非常不安全 - 包含的文档作为 PHP 处理。但是,file_get_contents 不能注入任何 PHP 代码,因此它非常安全(如果远程站点向您发送错误的 JavaScript 代码,则客户端的东西除外,例如 XSS)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多