2013-08-26 102 views

回答

2

首先下载JS文件https://github.com/padolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js并在您的页面中包含js文件。以下是我用来加载外部页面的功能。

function test() { 
    $.ajax({ 
     url: 'http://external_site.com', 
     type: 'GET', 
     success: function(res) { 
     var content = $(res.responseText).text(); 
     alert(content); 
     } 
    }); 
    } 

这适用于我从外部网站获取内容。

Reference

+0

我之前尝试过,它没有工作:(。任何想法,我错了吗? – NoSense

+0

不幸的是,这并没有把戏。 – NoSense

+0

对于后来的答案感到抱歉。 – NoSense

3

是的,看到http://api.jquery.com/load/ “加载页面片段”。

总之,你在URL后添加选择器。例如:

$('#result').load('ajax/test.html #container'); 
+0

感谢您的回答,但这并不奏效。 – NoSense

0

您可以尝试的iframe:根据岗位

<iframe src="http://en.wikipedia.org/wiki/Robert_Bales" frameborder="0"></iframe> 
+0

谢谢,但我只想获得页面某些元素的内容。感谢 – NoSense

1

您可以随时使用CURL在PHP中,以从其他站点获取内容,然后可以使用load()JQuery函数访问PHP并将其显示给该页面上的用户。 假设这个文件被命名为:“gatherinfo.php”(JQuery无法从远程站点收集信息,因为它不安全,我相信(http://forum.jquery.com/topic/using-ajax-to-load-remote-url-not-working)),所以其中一种方法是通过PHP来完成。

<?php 
$myData = curl("http://en.wikipedia.org/wiki/Robert_Bales"); 
echo "$myData"; 

function curl($url) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 
?> 

但是,确保您在收集这类信息时始终引用来源。 然后通过使用JQuery,你可以打电话给你的服务器上的文件和你想通过执行以下操作在div显示它:

的Javascript:(这将让初始化一次页面已准备就绪,并已加载)

$(document).ready(function(){ 
    $("#yourDIVID").load("gatherinfo.php"); 
}); 

HTML:

<html> 
<head> 
<script src="yourjqueryfile.js" type="text/javascript"></script> 
<script type="text/javascript"> 
     $(document).ready(function(){ 
      $("#yourDIVID").load("gatherinfo.php"); 
     }); 
</script> 
</head> 
<body> 
<div id="yourDIVID"> The content you're grabbing would load here </div> 
</body> 
</html> 

这是你会怎么做,如果这是你打算做什么。

+0

,但是如何获得某个元素的内容? – NoSense

+0

好吧,我加: $(“#getPrice”)。load(“gatherinfo.php #content”);但id似乎不起作用。任何想法,我错了? – NoSense

+1

向我们显示您的HTML。 Hashtags(#)用于指向id,而点(。)用于指向元素的类。我会为你编辑我的答案,并且也会为你做javascript函数。 –

相关问题