我一直在玩自制的UPNP/DLNA瀏覽功能。瞭解UPnP/DLNA上的CORS瀏覽
我設法做的是一個基於shell腳本的方法,使用curl查詢服務器和xsl處理,使html頁面出現在答案中。
接下來我想我可以將所有這些構建到javascript /種交互式瀏覽器頁面中。但是現在我遇到了CORS問題,因爲瀏覽器預先發現了這些請求(它們在使用curl時沒有預先指明,服務器也沒有說CORS,只是UPnP)。 試圖讓upnp的樹根一些簡單的代碼:...
<html>
<head>
<script language="javascript">
function newx() {
var h = new XMLHttpRequest();
h.open("POST", "http://hcds6106:50001/ContentDirectory/control", true);
h.onreadystatechange = processme;
h.setRequestHeader("SOAPACTION",'"urn:schemas-upnp-org:service:ContentDirectory:1#Browse"');
h.setRequestHeader("Content-Type", "text/xml; charset='utf-8'");
h.send('<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:ns0="urn:schemas-upnp-org:service:ContentDirectory:1" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ns0:Browse><ObjectID></ObjectID><BrowseFlag>BrowseDirectChildren</BrowseFlag><Filter>*</Filter><StartingIndex>0</StartingIndex><RequestedCount>0</RequestedCount><SortCriteria /></ns0:Browse></s:Body></s:Envelope>');
}
function processme()
{
alert(this.status);
}
</script>
</head>
<body onload="newx();"/>
</html>
顯然,瀏覽器(Firefox 47.0)擊中
Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource at http://hcds6106:50001/ContentDirectory/control.
(Reason: CORS header 'Access-Control-Allow-Origin' missing).
有任何合理的方式,我可以告訴我的瀏覽器在這種情況下跳過CORS的東西,因爲服務器不是它的?對於Firefox或任何其他常見的UserAgent?我不敢相信沒有瀏覽器就可以這麼簡單,並且幾乎不可能在瀏覽器內部實現互動...... TIA!
不表示不,但有很多解決方法;使用php代理,使用nw.js構建應用程序,使用擴展名,使用命令行參數禁用瀏覽器的安全性等。我爲這種用途保留了一個可移植副本。 – dandavis
好的,謝謝,看起來像[CorsEverywhere](https://addons.mozilla.org/en-US/firefox/addon/cors-everywhere/?src=search)firefox addon可以完成這項工作 –