我正在我的应用程序中实现signalr。我正在 No' Access-Control-Allow-Origin'标头出现在请求的资源上。连接到已部署的 signalR集线器时出现错误。
我需要在我的应用程序中使用signalr hub(localhost)。
我使用下面的代码: HUB方面:
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
// You can enable JSONP by uncommenting line below.
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
EnableJSONP = true,
EnableDetailedErrors = true,
EnableJavaScriptProxies= true
};
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
配置:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:64787/" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true"/>
</customHeaders>
</httpProtocol>
</system.webServer>
UI方面:
// signalR integration
var con = $.hubConnection('https://xxxx.azurewebsites.net/hub');
con.logging = true;
var hub = con.createHubProxy('hudHub');
con.starting(function () {
});
con.start()
.done(function () {
alert('Connected');
})
.fail(function () {
alert('Failed connection');
});
错误:
Failed to load https://xxxxx.azurewebsites.net/hub/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22hudhub%22%7D%5D&_=1524304303074: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:64787' is therefore not allowed access.