SignalR 1.0.1与Chrome的跨域请求(CORS)

时间:2013-03-17 23:05:18

标签: jquery asp.net-mvc google-chrome signalr cors

我正在尝试使用SignalR 1.0.1与Chrome(Ver 25.0.1364.172)实现跨域调用。因为我在一个主机(localhost:16881)中有我的UI,在另一个主机(localhost:16901)中有'service'。

我已按照主题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.

我还为globalRas中的路由映射设置了HubConfiguration,用于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启动它来使用Chrome,但它并不符合我的要求。请帮忙!

2 个答案:

答案 0 :(得分:11)

以下是您需要做的事情:

  1. 删除jQuery.support.cors = true
  2. 删除<add name="Access-Control-Allow-Origin" value="http://localhost:16881" />
  3. 然后它应该可以正常工作。

答案 1 :(得分:0)

您不需要使用

  

jQuery.support.cors = true;

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

$vehicles = array('Car','Bike','Plane','Bus');
$this->db->where_in('vehicle', $vehicles); //assuming vehicle is the name of a
//column in your database. Result will be:
// WHERE vehicle IN ('Car', 'Bike', 'Plane', 'Bus');