2013-03-17 109 views
7

我想用Chrome實現與SignalR 1.0.1的跨域調用(Ver 25.0.1364.172)。因爲我在一臺主機(本地主機:16881)中擁有我的用戶界面,而在另一臺主機(本地主機:16901)中擁有「服務」。SignalR 1.0.1與Chrome的跨域請求(CORS)

我一切準備就緒爲主題How to use cross-domain connections (CORS - Access Control Allow Origin) with SignalR

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. 

我也有HubConfiguration設置爲我的路由映射在Global.asax中的SignalR 1.0.1

  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啓動它,但它確實不符合我的要求。請幫忙!

回答

11

這裏就是你需要做的:

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

那麼它應該工作的罰款。

+0

太棒了!它的工作!你知道爲什麼jQuery.support.cors = true並且Access-Control-Allow-Origin會導致Chrome的問題嗎? – Adamy 2013-03-18 00:01:30

+0

我的猜測是你爲Access-Control-Allow-Origin設置了錯誤的東西,而且jQuery.support.cors = true。 – davidfowl 2013-03-18 03:46:41

+1

將jQuery.support.cors設置爲true會禁用JSONP,因爲它會導致SignalR假定瀏覽器支持CORS。 – halter73 2013-03-18 04:29:52

0

你並不需要使用

jQuery.support.cors = TRUE;

相反,您可以在配置方法的啓動類上啓用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); 
     });