Evgeni
Evgeni

Reputation: 3343

SignalR across domains: errors with not allowed by Access-Control-Allow-Origin

Trying to call signalR from another domain, and keep on getting this error:

XMLHttpRequest cannot load localhost:62150/signalr/negotiate?_=1362242757692. Origin localhost:4982 is not allowed by Access-Control-Allow-Origin.

This is the code I'm trying to run:

$(function () {
    jQuery.support.cors = true;
    $.connection.hub.url = 'http://localhost:62150/signalr';
    $.connection.hub.start()
        .done(function () { alert("Now connected!"); })
        .fail(function () { alert("Could not Connect!"); });
});

jquery and jquery.signalr.js are loaded, localhost:62150/signalr/hubs responds with JS, localhost:62150/signalr/hubs/negotiate?_=1362243021215 returns JSON if I run this in browser - so its not a missing script or invalid path issue.

What I've tried:

And combinations of all of the above.

Anyone has any idea what else I can try ?

The serving app is a combination of MVC and WebAPI (don't think it makes any difference). If I'm trying that code from same domain - it works.

Upvotes: 2

Views: 4868

Answers (2)

nsimeonov
nsimeonov

Reputation: 744

After wasting a couple of hours I think it's good to share my experience:

  1. DO NOT add Access-Control-Allow-Origin to your web.config (yes it never sais to add it, but when trying things this is literally the first I did and left it there after a simple jquery cross-domain access test)

  2. RouteTable.Routes.MapHubs( new HubConfiguration() { EnableCrossDomain = true } ); works just fine for classes inheriting Hub

  3. RouteTable.Routes.MapConnection<MyConnection>( "foo", "/foo", new ConnectionConfiguration { EnableCrossDomain = true } ); this works just fine too for classes inheriting PersistentConnection

Upvotes: 3

el_tone
el_tone

Reputation: 1192

If you are using 1.0 or higher have you enabled cross domain on the server? (it's disabled by default now)

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

Upvotes: 5

Related Questions