簡體   English   中英

如何啟用CORS

[英]How to enable CORS

我有兩個站點:本地:20279 /和本地:54220。 從20279的IFrame中打開了54220。用戶單擊按鈕后,將關閉IFrame,並在52220中獲取數據。我了解了如何啟用CORS,這是我在web.config中所做的:

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*"/>
            <add name="Access-Control-Allow-Headers" value="Content-Type" />
            <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

並且此代碼在文件js中讀取IFrame中的數據

function dialog_close() {
    dialog(false, '');

    getValueFromIFrame("if-dialog", "status");
}

function getValueFromIFrame(idIFrame, idField) {
    var iframe = document.getElementById(idIFrame);
    var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
    var value = innerDoc.getElementById(idField).value;
    alert(value);
    //return value;
}

這是我從瀏覽器中得到的:

Blocked a frame with origin 20279 from accessing a frame with origin "http://localhost:54220". Protocols, domains, and ports must match. <br/>
Uncaught SecurityError: Blocked a frame with origin "http://localhost:20279" from accessing a frame with origin 54220. Protocols, domains, and ports must match.

我的問題是:如何使它起作用?

謝謝

必須將響應中的Access-Control-Allow-Origin標頭設置為HTTP請求的Origin標頭中發送的確切值,*是不夠的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM