7

我正在尝试使用 SignalR 1.0.1 和 Chrome(版本 25.0.1364.172)实现跨域调用。因为我在一个主机(localhost:16881)中有我的用户界面,而在另一台主机(localhost:16901)中有“服务”。

我已经准备好了主题如何使用 SignalR 的跨域连接(CORS - 访问控制允许来源)

 add jQuery.support.cors = true; before opening a connection
set up $.connection.hub.url = 'http://localhost:16901/signalr';, pointing to your subdomain

allow cross-domain requests on server side, by adding the following header description:

<add name="Access-Control-Allow-Origin" value="http://localhost:16881" />

inside system.WebServer/httpProtocol/customHeaders section in Web.config file.

我还为 SignalR 1.0.1 的 global.asax 中的路由映射设置了 HubConfiguration

            RouteTable.Routes.MapHubs(new HubConfiguration()
            {
                EnableCrossDomain = true
            });

在 IE10 和 FF22 中一切看起来都很好。但是在 Chrome 中,当 SignalR 尝试握手时,它给了我一个错误。

XMLHttpRequest cannot load http://localhost:16901/signalr/negotiate?_=1363560032589. Origin http://localhost:16881 is not allowed by Access-Control-Allow-Origin. 

我知道我可以通过使用 --disable-web-security 启动它来使其与 Chrome 一起使用,但它并不真正符合我的要求。请帮忙!

4

2 回答 2

11

这是您需要做的:

  1. 删除 jQuery.support.cors = true
  2. 消除<add name="Access-Control-Allow-Origin" value="http://localhost:16881" />

然后它应该可以正常工作。

于 2013-03-17T23:24:01.720 回答
0

你不需要使用

jQuery.support.cors = true;

相反,您可以在 Configuration 方法的 Startup 类上启用 CORS 支持。下面是一些例子:

        // Branch the pipeline here for requests that start with "/signalr"
        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
            };
            // Run the SignalR pipeline. We're not using MapSignalR
            // since this branch already runs under the "/signalr"
            // path.
            map.RunSignalR(hubConfiguration);
        });
于 2017-05-19T19:02:57.537 回答