我正在写一个简单的应用程序来从bbc rss feed中删除新闻报道http://feeds.bbci.co.uk/news/rss.xml。为什么我的XMLHttpRequest不允许XSS?
它需要完全在客户端上运行,而不是使用jQuery,所以JSONP不是一个可行的解决方案。我一直在使用本地主机上的IE进行测试,并且在检测到跨站点请求时单击弹出的“允许内容”按钮。 Chrome和Firefox并不是那么容易让他们接受这一点,现在我想在这些浏览器上进行测试,看看我的应用是否适用于他们。
到目前为止..... 我试图改变我的JavaScript使用像这样一个CORS请求......
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;
}
var xhr = createCORSRequest('GET', feedURL);
\t xhr.withCredentials = true;
\t if (!xhr) {
\t throw new Error('CORS not supported');
\t }
\t xhr.onload = function() {
\t if (xhr.status === 200) {
\t \t var xmlDoc;
\t \t \t if (window.DOMParser){
\t \t \t \t parser = new DOMParser();
\t \t \t \t xmlDoc = parser.parseFromString(xhr.responseText,"text/xml");
\t \t \t }
\t \t \t else{ // Internet Explorer
\t \t \t \t xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
\t \t \t \t xmlDoc.async=false;
\t \t \t \t xmlDoc.loadXML(xhr.responseText);
\t \t \t }
\t \t \t //do some stuff
\t }
\t else {
\t alert('Request failed. Returned status of ' + xhr.status);
\t }
\t };
\t xhr.send();
我也上传到我的Web服务器,并与IIS 6托管它。我添加了一个Web配置,与这些设置。
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
\t <httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<defaultDocument>
<files>
<add value="rss.html" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
我发现有关在IIS中设置处理程序映射的文章。建议将OPTIONSVerbHandler设置为ISAPI ...但是我没有这个设置。
任何人都可以对此有所了解。我将不胜感激。