我正在编写一个简单的应用程序,用于从 http://feeds.bbci.co.uk/news/rss.xml 的 BBC RSS feed 中提取新闻报道。 .
它需要完全在客户端上运行,而不是使用 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);
xhr.withCredentials = true;
if (!xhr) {
throw new Error('CORS not supported');
}
xhr.onload = function() {
if (xhr.status === 200) {
var xmlDoc;
if (window.DOMParser){
parser = new DOMParser();
xmlDoc = parser.parseFromString(xhr.responseText,"text/xml");
}
else{ // Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(xhr.responseText);
}
//do some stuff
}
else {
alert('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send();
我还将其上传到我的 Web 服务器并使用 IIS 6 托管它。我添加了一个带有这些设置的 Web 配置。
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<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 ...但我没有该设置。
任何可以阐明这一点的人。我将不胜感激。
最佳答案
经过更多研究。看来最简单的解决方案是创建我自己的代理。
- 将静态网站转换为空白 ASP.Net Web 应用程序
- 在项目中创建一个通用处理程序,用于从服务器联系 bbc feed
- 从客户端 JS 调用该处理程序
这是我的处理程序,供任何感兴趣的人使用
using System;
使用System.Collections.Generic; 使用系统.IO; 使用 System.Linq; 使用System.Net; 使用系统.Web; 使用 System.Xml;
命名空间提要 { /// ///rss的摘要描述 /// 公共(public)类 rss:IHttpHandler {
public void ProcessRequest(HttpContext context)
{
string locationsRequest = CreateRequest();
context.Response.Write(locationsRequest);
}
public bool IsReusable
{
get
{
return false;
}
}
public static string CreateRequest()
{
return XmlHttpRequest("http://feeds.bbci.co.uk/news/rss.xml", "");
}
public static string XmlHttpRequest(string urlString, string xmlContent)
{
string response = null;
HttpWebRequest httpWebRequest = null;//Declare an HTTP-specific implementation of the WebRequest class.
HttpWebResponse httpWebResponse = null;//Declare an HTTP-specific implementation of the WebResponse class
//Creates an HttpWebRequest for the specified URL.
httpWebRequest = (HttpWebRequest)WebRequest.Create(urlString);
try
{
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(xmlContent);
//Set HttpWebRequest properties
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = bytes.Length;
httpWebRequest.ContentType = "text/xml; encoding='utf-8'";
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
//Writes a sequence of bytes to the current stream
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();//Close stream
}
//Sends the HttpWebRequest, and waits for a response.
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
//Get response stream into StreamReader
using (Stream responseStream = httpWebResponse.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
response = reader.ReadToEnd();
}
}
httpWebResponse.Close();//Close HttpWebResponse
}
catch (WebException we)
{ //TODO: Add custom exception handling
throw new Exception(we.Message);
}
catch (Exception ex) { throw new Exception(ex.Message); }
finally
{
httpWebResponse.Close();
//Release objects
httpWebResponse = null;
httpWebRequest = null;
}
return response;
}
}
}
关于javascript - 为什么我的 XMLHttpRequest 不允许 XSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30442492/