2016-01-06 74 views
1

我有兩個頁面,一個在另一個頁面的iframe中運行:test.php在check.php中運行。 Test.php是作爲代理來發揮作用以避開交叉來源。 iframe正常工作並提供正確的值。但是,當我嘗試從父頁面中的div ID獲取值時,我得到以下響應: 未捕獲的SecurityError:阻止了源於「http://localhost」的框架訪問源於「http://www.allow-any-origin.appspot.com」的框架。協議,域和端口必須匹配。從iframe獲取div值

這裏是我的代碼:

check.php

<iframe id="vd" src="http://localhost/vd/test.php/test.php" width="720"  
    height="300" frameborder="0" scrolling="no" allowtransparency="true"> 
    </iframe> 
    <form> 
    <input id="test" type="button" value="Submit"> 
    </form> 

    <script type="text/javascript"> 
    jQuery('#test').click(function() { 
    var mb = 
    document.getElementById('vd').contentWindow.document.getElementById 
    ('#saldo_result'); 
    });   
    </script> 

test.php的

<form action="http://www.allow-any- 
    origin.appspot.com/http://vend.giftcardservices.nl/saldo/check? 
    show_form=0&card_id=3005&card_id=&ajax=1" method="GET"> 
    <input type="text" name="card_id" value="3005"> 
    <input type="text" name="card_id" value=""> 
    <br> 
    <input type="submit" value="Submit"> 

我以爲這條路是讓瀏覽器接受這種方式。任何人都可以看到我要去哪裏嗎?

+0

如果該網站正在設置CORS,請使用Ajax。如果不在您的域上創建代理來獲取它。 – epascarello

+0

這就是我認爲我這樣做的原因。 – user3069332

回答

0

我做了這樣的事情從php。

客戶端< - > yourPostScript.php < - >外部網頁。

將發佈數據發佈到yourPostScript.php,發佈到外部allow-any-origin.appspot.com,您可以在yourPostScript.php中獲得響應,並且您可以對其進行迴應。

對不起,我希望這不是ilegal ..

- 編輯 -

test.php的代碼:

<form action="yourPostScript.php" method="GET"> 
<input type="text" name="card_id" value="3005"> 
<input type="text" name="card_id" value=""> 
<br> 
<input type="submit" value="Submit"> 

yourPostScript.php代碼:(只是一個片段,它應該用於示範)

<?php 
/* validating method and fields i think is enough for ignoring images and others..*/ 
    if($_SERVER['REQUEST_METHOD']!='POST') die; 
    if(!$_POST['card_id']) die; 

    $url = 'http://vend.giftcardservices.nl/saldo/check? 
    show_form=0&card_id=3005&card_id=&ajax=1'; 
    $data = array('card_id' => $_POST['card_id'], 'other_field' => $_POST['other_field']); 

// use key 'http' even if you send the request to https://... 
$options = array(
    'http' => array(
     'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
     'method' => 'POST', 
     'content' => http_build_query($data), 
    ), 
); 
$context = stream_context_create($options); 
echo $result = file_get_contents($url, false, $context); ?> 
+0

這種方式後的迴應仍然是您的網站,您可以訪問它 – user3100334

+0

您可以發佈到任何你想要的網站..這是非法的,除非用於惡意的意圖。如果網站所有者不希望它發生,他們會看看頭和鎖在這個問題上,許多API以這種方式工作,從POST或GET請求跨域產生內容。 – Zak

+0

這是不是我在中間使用test.php所做的?或者我誤解了你? – user3069332

2

JavaScript不會達到框架...... JS的標準設置得非常高。這樣,我不只是服務別人的頁面作爲iFrame,並使用我的JavaScript來收集登錄信息等等等等等等等等。等等。

+0

那麼,它只是取一個數字輸出值,並能夠用它來計算。沒有不好的意圖是肯定的。 – user3069332

+0

不好意思。如果它被「允許」,那麼這將是任何人都可以利用的安全漏洞。你的意圖沒有問題。出於安全原因,這是不可能的。 – Zak

+0

啊,清楚。所以沒有辦法做到這一點? – user3069332