无需 XMLHttpRequest 即可获取 URL 的 HTML

发布于 2024-12-25 02:44:15 字数 346 浏览 0 评论 0原文

感谢您的阅读。我正在尝试提出一个 Javascript 函数,它将外部 URL 处的页面的 HTML 源代码转换为变量,以便整个内容变得可编辑。复杂的是,URL 并不是以“html、htm、aspx”扩展名结尾,而是以一串输入表单变量结尾(即?type=AAA&color=BBB...)。因此 XMLHttpRequest 方法是不可能的。

这在 JS/jQuery 中可行吗?我听说过同源策略,但以下工具设法做到了这一点,尽管是在 PHP 中: http:// /www.iwebtool.com/code_viewer

thanks for reading. I'm trying to come up with a Javascript function that would convert the HTML source of a page at an external URL into a variable, so that the whole thing would become editable. The complication is, the URL does not end with a "html, htm, aspx" extension, but instead with a string of input form variables (i.e. ?type=AAA&color=BBB...). Hence the XMLHttpRequest method is out of the question.

Is this doable in JS/jQuery at all? I've heard about the same origin policy, but the following tool manages to do just that, although in PHP: http://www.iwebtool.com/code_viewer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

緦唸λ蓇 2025-01-01 02:44:15

同源策略确实适用于这种情况,但是您可以通过服务器端代码 (PHP) 和 jQuery 的组合来实现。这是一个小例子。

PHP

<?php
    $url = $_REQUEST['url'];
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,$url);
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);
    echo($buffer);
?>

jQuery / HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
  type: "POST",
  url: "yourPhpScript.php",
  data: "url=http://stackoverflow.com"
}).done(function( content ) {
  $('#content').html(content);
  //content is your variable containing the source
});
</script>

<div id="content"></div>

Same origin policy does apply in this case, however you can do it with a combination of server side code (PHP) and jQuery. Heres a little example.

PHP

<?php
    $url = $_REQUEST['url'];
    $curl_handle=curl_init();
    curl_setopt($curl_handle,CURLOPT_URL,$url);
    curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);
    echo($buffer);
?>

jQuery / HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
  type: "POST",
  url: "yourPhpScript.php",
  data: "url=http://stackoverflow.com"
}).done(function( content ) {
  $('#content').html(content);
  //content is your variable containing the source
});
</script>

<div id="content"></div>
冷夜 2025-01-01 02:44:15

XMLHttpRequest 适用于任何有效的 url,只需为其提供适当的 url,您就可以获得文本形式的响应。

但是,存在同源策略的限制。对于不同的情况,有不同的解决方法,但如果您希望能够操纵收到的文本,那么实际上只有一个选择。使用与当前相同的 javascript,只需将其添加为 getUrl 的第一行:

url='/path/to/proxy.php?url='+encodeURIComponent(url);

然后,在您的服务器(为页面及其 javascript 提供服务的同一服务器)上,编写 proxy.php:

<?php
  echo file_get_contents($_GET['url']);
?>

这将使您发出的每个 ajax 请求转到您的服务器,该服务器没有仅从一个域加载的限制。服务器将加载您请求的 url,并用从加载的页面获得的响应回复您。请注意,上面的脚本只会为您提供内容正文(您在查看源代码时看到的内容) - 如果您需要访问 HTTP 标头,您也可以中继这些标头,但这会更加复杂。

XMLHttpRequest works with any valid url, just give it the appropriate url and you can get the response as text.

However, there is the restriction of the same-origin policy. There are different workarounds to this for different situations, but if you want to be able to manipulate the text you receive then there is really only one option. Use the same javascript as you currently have, just add this as the first line of getUrl:

url='/path/to/proxy.php?url='+encodeURIComponent(url);

Then, on your server (the same one that's serving the page and its javascript), write proxy.php:

<?php
  echo file_get_contents($_GET['url']);
?>

This will make every ajax request you make go to your server, which does not have the restriction of loading from only one domain. The server will load the url you asked for, and reply to you with the response it got from the page it loaded. Note that the above script will only give you the content body (what you see when you view-source) - if you need to access HTTP headers you can relay those too, it will just be more complicated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文