【发布时间】:2016-12-21 03:42:21
【问题描述】:
我一直在玩自制的 UPNP/DLNA 浏览。
我设法做的是一种基于 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 构建应用程序,使用扩展程序,使用命令行参数禁用浏览器上的安全性等。我为此类用途保留了 chrome 的可移植副本。
-
好的,谢谢,看起来 CorsEverywhere firefox 插件可以完成这项工作
标签: javascript ajax cors upnp