2012-02-25 119 views
0

我想從其他網站上的元素獲取內容。然後,我想將該內容顯示在同一臺服務器上的另一個網站上。使用jQuery(跨域)從其他網站獲取所有內容

我可以通過YQL插件和jQuery獲得它,但我只能從某些元素中獲取內容,而不是從所有元素中獲取內容。

如何從所有元素獲取內容?

我被告知必須先使用PHP加載所有內容,然後使用jQuery顯示結果。怎麼做?

+0

幫助你試過IFrame嗎? :) – dbrin 2012-02-25 05:52:26

+0

從來沒有聽說過嗎?它是一個插件嗎? – Bayu 2012-02-25 05:53:24

+1

大聲笑 - 你讓我的一天:) – dbrin 2012-02-25 05:55:30

回答

0

如果您可以選擇使用服務器端語言,那麼您可以創建代理,讓您的代理調用該網站並以xml或json格式將結果返回給您。使PHP文件調用它proxy.php該文件內從跨域獲取數據

proxy.php

<?php 
function get_url_contents($url){ 
     $crl = curl_init(); 
     $timeout = 5; 
     curl_setopt ($crl, CURLOPT_URL,$url); 
     curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); 
     $ret = curl_exec($crl); 
     curl_close($crl); 
     return $ret; 
} 

    $url = 「http://www.howtogeek.com」; 
    $str = file_get_contents($url); 

    echo $str; 
?> 

客戶端

$.ajax({ 
url:'proxy.php', 
dataType:'text\xml', 
success:function(data){//success handler 
//if the data is in xml format parse it using $.parseXML 
//do something 
}, 
error:function(jxhr){ //error handler 
    console.log(jxhr.responseText); 
} 
}); 

上面的代碼僅用於示範目的,我已經從here

+0

謝謝,我會嘗試它,讓你知道結果後 – Bayu 2012-02-25 06:18:05

+0

好吧,我已經試過了。它不會返回所有內容。結果與YQL一樣,只有一些內容被獲取。 – Bayu 2012-02-26 07:38:40