短:
XSLT应用于XML,我希望使用{X}和原始XML之外的其他域使用document(http://...)
加载另一个XML。我在服务器上添加了CORS标头,它适用于Firefox,而不适用于Chrome。为什么以及如何解决这个问题?
完整案例:
我首先使用html5Rocks示例尝试了CORS请求。所以我有一个html文档,http://localhost/cors.html
包含以下代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>
</title>
<script>
function createCORSRequest(method, url)
{
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr)
{
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
}
else if (typeof XDomainRequest != "undefined")
{
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
}
else
{
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
}
function go()
{
console.log('go!');
var url = 'http://cors1.localhost/cors-data.xml';
var xhr = createCORSRequest('GET', url);
if (!xhr)
{
throw new Error('CORS not supported');
}
xhr.onload = function()
{
var responseText = xhr.responseText;
var responseXml = xhr.responseXML;
console.log(responseXml);
// process the response.
};
xhr.onerror = function()
{
console.log('There was an error!');
};
xhr.send();
}
document.addEventListener('DOMContentLoaded', go, false);
</script>
</head>
<body>
</body>
</html>
在firefox上正常工作:XHR对象发送CORS请求,浏览器和服务器都能很好地处理它,这要归功于以下服务器的.htaccess文件:
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Credentials "true"
Header set Access-Control-Allow-Methods "OPTIONS, GET, POST"
Header set Access-Control-Allow-Headers "Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control"
现在,我在Chrome上测试它......没问题,它也可以正常工作☺
在这两种浏览器中,控制台都会显示XHR响应的内容(responseXml
),所以我假设服务器配置得很好(不是吗?)。
现在,我有一个XML文件:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="CORS.xsl"?>
<cors source="http://cors1.localhost/CORS-data.xml"/>
XSLT应用于它:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="/cors">
<xsl:variable name="cors" select="document(@source)/cors"/>
<p>
<xsl:text>CORS-data.xml (</xsl:text>
<a href="{@source}">
<xsl:value-of select="@source"/>
</a>
<xsl:text>): </xsl:text>
<xsl:value-of select="$cors"/>
</p>
</xsl:template>
</xsl:stylesheet>
因此,XSLT应加载外部文档(http://cors1.localhost/CORS-data.xml
)并显示其内容(<xsl:value-of select="$cors"/>
)。它实际上在Firefox中做得很好,但在Chrome中没有,控制台说:
Unsafe attempt to load URL http://cors1.localhost/CORS.xsl from frame with URL http://localhost/CORS.xml. Domains, protocols and ports must match.
页面结果为:
CORS-data.xml (http://cors1.localhost/CORS-data.xml):
在:
之后应该有document() - 加载的XML内容(一个简单的'ok'),但它是无效的。
我看到了几个关于此类问题的主题,但它们是file:///
协议,而不是http://
。我可以理解,file:///
XSLT由于安全原因而被禁止,但我没有得到
为什么Chrome的CORS可以与javascript的XHR配合使用,但是使用XSLT的document()
功能失败了?以及如何解决这个问题?
答案 0 :(得分:-1)
Chrome不允许将localhost作为源,您应该尝试使用--allow-file-access-from-files和--disable-web-security标志启动Chrome。