2011-02-02 82 views
2

我有一個iframe中這個文件上傳,但是當我把它嵌入到其他網站不允許我,螢火顯示此錯誤:的Iframe上傳權限

Permission denied for < http://www.mywebsite.com > to get property Window.document from < http://www.myotherwebsite.com >.

裏面說到這行:

$('iframe', top.document).css('border', '1px green solid'); 

我試圖在上傳完成後用邊框設置iframe的樣式。

我看到了其他問題,解決方案是做一個服務器端代理,我不知道如何讓它的代理工作,並允許執行jQuery。

乾杯。

賞金添加。

回答

5

服務器端代理可能會幫助您解決此問題。雖然瀏覽器只能使用同一個域對其服務器進行AJAX調用,但服務器本身可以無限制地與任何其他服務器進行通信。

假設您需要向雅虎的Weather API發出AJAX請求。由於相同的域策略,您無法從www.example.com向www.yahoo.com發出請求,所以解決方法是先打電話給您的服務器,然後讓您的服務器向雅虎發出請求。下面是一個代理,做到了這一點的例子:

請求:http://www.example.com/weather.php?zip=97015

<? // Yahoo Weather proxy 

$zip_code = $_REQUEST["zip"]; 

// default to Portland, OR for testing 
if($zip_code == null || $zip_code == '') 
    $zip_code = "97206"; 

// delegate request to the other server, circumventing the same-domain policy. 
$request = "http://weather.yahooapis.com/forecastrss?p=".$zip_code; 

$response = file_get_contents($request); 

// Retrieve HTTP status code 
list($version,$status_code,$msg) = explode(' ',$http_response_header[0], 3); 


// Check the HTTP Status code 
switch($status_code) { 
    case 200: 
      // Success 
      break; 
    case 503: 
      die('Your call to Yahoo Web Services failed and returned an HTTP status of 503. That means: Service unavailable. An internal problem prevented us from returning data to you.'); 
      break; 
    case 403: 
      die('Your call to Yahoo Web Services failed and returned an HTTP status of 403. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit.'); 
      break; 
    case 400: 
      // You may want to fall through here and read the specific XML error 
      die('Your call to Yahoo Web Services failed and returned an HTTP status of 400. That means: Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.'); 
      break; 
    default: 
      die('Your call to Yahoo Web Services returned an unexpected HTTP status of:' . $status_code); 
} 


echo $response; 
?> 

現在,你的情況,你要當文件上傳完成樣式的iframe。一個簡單的解決方案是輪詢父文檔的服務器,並讓代理輪詢上傳服務器,直到找到文件。一旦找到該文件,可以使用該響應來調用改變iframe樣式的JQuery代碼。

爲了使代理概念發揮作用,您嵌入上傳器的每個網站都需要部署自己的同域代理,以檢查您的上傳網站是否存在該文件,然後將該響應返回給客戶。

父文檔也會以某種方式需要知道正在上傳的文件的名稱。由於具有相同的域策略,您可能無法確定文件名,這在使用代理來檢查文件的存在時會帶來挑戰。你怎麼知道你在檢查什麼?

0

您好我在前一天遇到一篇文章,同時試圖找到一種方法來在iFrames和使用名稱加載它們的窗口之間來回交談。

http://softwareas.com/cross-domain-communication-with-iframes

演示 - http://ajaxify.com/run/crossframe/duo/

// site 1 code 
<iframe name="frame1" src="site2"> 

所以說,你的第一個站點載入你的第二個站點以上的IFRAME。第二個站點代碼應該添加此代碼。

//site 2 code 
$(something).load('url', function() { 
    parent.frames["frame1"].css('border', '1px green solid'); 
}); 

我相信你也可以讓函數調用到iFrame從現場1:

//site 1 code 
parent.frames["frame1"].functionName(variables); 
+0

如果框架來自其他域 – 2011-02-06 14:38:45

+0

我不知道該如何猜測你沒看過演示。因爲當您檢查頁面上的元素時,您會注意到與父頁面交互的iFrame來自另一個域。 – 2011-02-07 20:14:19

0

服務器端代理

Apache配置

LoadModule proxy_module modules/mod_proxy.so 
LoadModule proxy_http_module modules/mod_proxy_http.so 

ProxyRequests Off 

<Proxy *> 
Order deny,allow 
Allow from all 
</Proxy> 

ProxyPass /foo http://mywebsite.com/ 
ProxyPassReverse /foo http://mywebsite.com/ 

我希望這有助於

所以如果你從www.bar.com創建一個請求到www.bar.com/foo,apache會傳遞到www.mywebsite.com

+0

當我將這個添加到我的`.htaccess`的底部時,我得到一個`500 Internal Error`。我相信你的解決方案可以工作,但只需要解決這個問題。 – MacMac 2011-02-06 15:09:56

0

我會以相反的方式去除邊界包含文檔,在iframe中添加假邊框並更改該內容,解決跨域問題。

+0

非常簡單易用的解決方案。 +1 – jmort253 2011-02-07 08:32:27

0

框架只是一些HTML內容嗎?無法將邊框直接添加到iframe中的body標籤周圍?

如果您擔心內容會因爲2個像素而移動,那麼只需在開頭放置一個相同的Siez的透明邊框即可。

0

您可以輕鬆地應用easyXDM這個問題 - 它甚至有一個demo如何使用響應數據進行跨域上傳。

this blog post我也解釋了它背後的基礎知識。

0

如果你擁有所有的網站,包括你的上傳服務器和你正在部署上傳器的網站,那麼也許一些簡單的DNS技巧可以幫助你克服。

  • 比方說,例如您的上傳服務器域是:upload.example.com
  • 假設你的服務器是www.example.com

在上述情況下,你可以通過設置document.domain屬性啓用跨站點腳本:

document.domain = "example.com"; 

這使得www.example.com與upload.example.com iframe來溝通。

假設您擁有網站,子域之間的通信能力可以幫助您在其他Web服務器之間進行通信。

如果域名不同,你會怎麼做?

假設如下:

  • upload.example.com是上傳服務器。
  • www.domain.com是您的網站,它是包含上傳器iframe的父文檔。

現在我們再次假設您擁有這兩個域名,或者至少可以訪問這些設置。使用我們上面介紹的有關在子域上啓用跨站點腳本的知識,您可以使用一些DNS技巧來提供幫助。

  • 在您的DNS管理器中,爲upload.domain.com創建一個CNAME,並將該子域指向與upload.example.com相同的服務器。完成後,upload.example.comupload.domain.com都指向相同的服務器和PHP應用程序。
  • www.domain.com,嵌入upload.domain.com並設置document.domain="domain.com";
  • www.example.com,嵌入upload.example.com &設置document.domain="example.com";

你可以看到,在這兩種情況下,你的網站的域名相匹配的上傳域名和文檔。域屬性與域匹配。

當您撥打$('iframe', top.document).css('border', '1px green solid');時,您將不會收到任何權限錯誤。

總之,只要確保無論您在哪個網站中嵌入上傳者iframe,您都爲該上傳者創建了與網站域匹配的CNAME別名,並且document.domain屬性在上傳者和網站。

可以使用document.referrer屬性在iframe來動態確定父文檔的背景下,以確定哪些域屬性應設置爲:

// uploader file code 

// array split by period to get domain ["http://uploader", "example", "com/iframe/uploadFile", "php"] 
var domainSplit = document.referrer.split("."); 

// the 2nd place in the array is the domain. You may need to improve this for deeper subdomains 
document.domain = domainSplit[1]; 

注:我假設你」重新使用Apache和PHP。如果是這樣,您可以爲您的所有upload.XYZ.com域創建ServerAlias條目。如果您使用的是其他服務器,則大多數服務器都有一些設置ServerAlias的方法。