加载外部网站的内容 [英] Load external site's content

查看:184
本文介绍了加载外部网站的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示另一个外部网站的内容到我的网站。通常情况下<的iFrame> 标签可以做到这一点。但是,我的要求是不是全部内容,该网站的一部分。例如该网站的布局有3个部分,< D​​IV ID =头> < D​​IV ID =侧边栏> < D​​IV ID =内容。我的意思是我想只显示ID =内容部分。我该怎么做呢?

I need to display another external site's content into my site. Normally <iFrame> tag can do it. But my requirement is not whole content, only part of that site. For example that site's layout has 3 parts, <div id="header">, <div id="sidebar">, <div id="content". I mean I want only display "id=content" part. How do I do it?

我试过 $($我的内容).load(http://www.anothersite.com#加载内容),但不工作

推荐答案

这是因为AJAX的跨域安全限制,一招是设置一个代理脚本 从该下载从不同的站点(域)的内容,并使用该代理在JavaScript参考服务器。

This is due to ajax cross domain security restrictions, one trick is to setup a proxy script from the server that the downloads the contents from different site(domain) and use that proxy as your reference in javascript.

例:(proxy.php)

Example: (proxy.php)

<?php
    $url = 'http://www.anothersite.com';
    $htm = file_get_contents($url);
    echo $htm;
?>

然后在你的脚本,而不是:

Then on your script, instead of:

$("$my-content").load("http://www.anothersite.com #load-content");

使用代理:

$("$my-content").load("proxy.php #load-content");

这篇关于加载外部网站的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆