SignalR CORS错误

问题描述 投票:1回答:1

当尝试通过SignalR从JavaScript客户端调用服务器端方法时,出现以下错误:

XMLHttpRequest cannot load http://localhost:49679/signalr/send?transport=serverSentEvents&clientProtoc…XWYPNm9Av7oD1DiEodZenKiQdpQFWUqDJ9vD0UgrYc%2FjZs21i3&connectionData=%5B%5D. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 500.

我正在构建一个简单的原型,这就是我在Web服务器上的Startup.cs类中的内容:

using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Owin;

[assembly: OwinStartupAttribute(typeof(WebRTCSignalServer.Web.Startup))]
namespace WebRTCSignalServer.Web
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
           //app.UseCors(CorsOptions.AllowAll);

            //Add SignalR
            app.Map("/signalr", map =>
            {
               map.UseCors(CorsOptions.AllowAll);

               // configure signalR
               HubConfiguration hubConfiguration = new HubConfiguration()
               {
                  EnableJavaScriptProxies = false,
                  EnableDetailedErrors = true
               };

               //GlobalHost.HubPipeline.AddModule(new ErrorHandlingPipelineModule());

               // Run the SignalR pipeline. We're not using MapSignalR
               // since this branch already runs under the "/signalr"
               // path.
               map.RunSignalR(hubConfiguration);
            });

            ConfigureAuth(app);
        }
    }
}

这是javascript信号器客户端代码:

var connection = $.hubConnection('http://localhost:49679/signalr', { useDefaultPath: false });
console.log(connection);

connection.stateChanged(function () {
    console.log('stateChanged.... state=' + connection.state);
});

connection.connectionSlow(function () {
    console.log('connectionSlow.... state=' + connection.state);
});

connection.reconnecting(function () {
    console.log('reconnecting.... state=' + connection.state);
});

connection.reconnected(function () {
    console.log('reconnected.... state=' + connection.state);
});

connection.disconnected(function () {
    console.log('disconnected.... state=' + connection.state);
});

connection.error(function (error) {
    console.log('error.... state=' + connection.state);
});

var webRTCSignalServerHub = connection.createHubProxy('webrtcsignalserver');

console.log(webRTCSignalServerHub);

connection.start().done(function(){
    webRTCSignalServerHub.invoke('register', 'wkst0314');
});

有人可以解释为什么会这样,如何解决?我在SignalR文档中遵循了此示例以启用CORS,但对我而言不起作用。不知道这是否重要,但我正在Chrome浏览器中尝试此操作:http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#crossdomain

提前感谢您的帮助。

javascript signalr owin signalr-hub
1个回答
1
投票

我能够弄清楚。我正在使用的signalR Hub的名称不正确,这就是发生此错误的原因。更正集线器名称后,一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.