NOTE: Someone else originally asked this question but deleted it before I could post my answer. Since this question covers many issues that developers face when trying to make SignalR work cross-domain, I decided to replicate it. Plus, I had already finished writing the answer!
I'm running a SignalR 1.0.1 server in an ASP.NET MVC .NET Framework 4 project. I have another ASP.NET application on a different domain (different localhost port) trying to connect via the JavaScript client. I get this when my application tries to connect:
XMLHttpRequest cannot load http://localhost:31865/api/negotiate?_=1363105027533.
Origin http://localhost:64296 is not allowed by Access-Control-Allow-Origin.
I've followed all steps to enable cross-domain support with SignalR -- what am I missing?
jQuery.support.cors = true;
$.connection('http://localhost:31865/api', '', false, { jsonp: true, xdomain: true });
RouteTable.Routes.MapHubs(new HubConfiguration { EnableCrossDomain = true });
RouteTable.Routes.MapConnection<ApiConnection>("/api", "api");
I also added the following to Web.config in the API project:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
I'm using a PersistentConnection for my SignalR server, not hubs.
Any ideas?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…