Asp.net mvc Chrome 27中的信号器和跨域请求问题
编辑:这显然只是Chrome中的一个问题,它在FF和IE中运行良好 Chrome版本:27.0.1453.116 我在localhost:13371上有一个简单的JS/HTML站点,我正在尝试访问localhost:13371上的信号器中心 无论何时提出请求,我都会收到以下错误: XMLHttpRequest无法加载Asp.net mvc Chrome 27中的信号器和跨域请求问题,asp.net-mvc,iis-7,cross-domain,signalr,Asp.net Mvc,Iis 7,Cross Domain,Signalr,编辑:这显然只是Chrome中的一个问题,它在FF和IE中运行良好 Chrome版本:27.0.1453.116 我在localhost:13371上有一个简单的JS/HTML站点,我正在尝试访问localhost:13371上的信号器中心 无论何时提出请求,我都会收到以下错误: XMLHttpRequest无法加载http://localhost:13370/signalr/hubs/negotiate?_=1372338722032。来源http://localhost:13371是访问控制
http://localhost:13370/signalr/hubs/negotiate?_=1372338722032
。来源<代码>http://localhost:13371是访问控制允许原点不允许的
我已经尝试过的:
- 启用
应用程序启动时信号器上的跨域:
RouteTable.Routes.MapHubs(new HubConfiguration { EnableCrossDomain = true });
- 在Signal server的Web.Config中启用跨域:
<system.webServer> <httpProtocol> <customHeaders> <clear /> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Methods" value="*" /> <add name="Access-Control-Allow-Headers" value="*" /> </customHeaders> </httpProtocol> </system.webServer>
- 在jQuery中启用CORS:
$.support.cors = true;
- 在
:应用程序中手动设置响应头\u BeginRequest
protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (HttpContext.Current.Request.HttpMethod == "OPTIONS") { //These headers are handling the "pre-flight" OPTIONS call sent by the browser HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "*"); HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); HttpContext.Current.Response.End(); } }
- 这两个站点都在Windows 7上的IIS中运行
- 信号器站点在端口13370上运行,作为.NET 4.0下MVC 4站点的一部分
- JS/HTML站点是端口13371上的一个简单Web服务器,根本没有托管代码
- SignalR站点肯定会响应浏览器对相关资源的直接请求
- 信号机版本为1.1.2
RouteTable.Routes.MapHubs(新的HubConfiguration{EnableCrossDomain=true})在应用程序启动中的code>
$.connection.hub.url=http://localhost:13370/signalr/hubs';代码>
//detect chrome
var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
//set the connection url.
$.connection.hub.url = 'http://localhost:13370/signalr/hubs';
//use jsonp if chrome
$.connection.hub.start({
jsonp: isChrome
});
可能有更好的方法来测试浏览器功能并相应地设置jsonp。。。看着用户代理,感觉像地狱一样肮脏。。。但这在过渡期间解决了我的问题。我希望这对其他人有帮助。这在文档中,但永远不要这样做:
$.support.cors=true 对我有效的方法如下
protected void Application_Start(object sender, EventArgs e)
{
var config = new HubConfiguration
{
EnableCrossDomain = true
};
RouteTable.Routes.MapHubs(config);
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (this.Context.Request.Path.Contains("signalr/"))
{
this.Context.Response.AddHeader("Access-Control-Allow-Headers", "accept,origin,authorization,content-type");
}
}
这对我有用。将此方法放在Global.asax.cs上
protected void Application_BeginRequest(object sender, EventArgs e)
{
Context.Response.AppendHeader("Access-Control-Allow-Credentials", "true");
var referrer = Request.UrlReferrer;
if (Context.Request.Path.Contains("signalr/") && referrer != null)
{
Context.Response.AppendHeader("Access-Control-Allow-Origin", referrer.Scheme + "://" + referrer.Authority);
}
}
我在MVC6 SignarR服务器上也遇到了同样的问题。
刚刚在软件包管理器控制台中安装了
Install Package Microsoft.Owin.Cors
,并在Startup.cs中添加了app.UseCors(CorsOptions.AllowAll)
你试过阅读吗?我做了与上面方法1相同的事情,虽然我没有收到任何错误,但我的客户拒绝连接到我的信号集线器。它进入了尝试与中心协商的步骤,但无法建立连接,因为结果对象的协议版本(从Ajax尝试建立连接时开始)与连接的clientProtocol属性不同。前者为1.4,后者为1.5。因此,连接被拒绝。这篇文章中的任何建议对我都不起作用。还有其他想法吗?
protected void Application_BeginRequest(object sender, EventArgs e)
{
Context.Response.AppendHeader("Access-Control-Allow-Credentials", "true");
var referrer = Request.UrlReferrer;
if (Context.Request.Path.Contains("signalr/") && referrer != null)
{
Context.Response.AppendHeader("Access-Control-Allow-Origin", referrer.Scheme + "://" + referrer.Authority);
}
}