Cross domain requests not working in SignalR 2.0.0-rc1

Something is wrong with your client configuration.

XMLHttpRequest cannot load =1377623738064">http://localhost:8080/negotiate?connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&clientProtocol=1.3&=1377623738064. Origin http://localhost:7176 is not allowed by Access-Control-Allow-Origin.

The negotiate request should be made to http://localhost:8080/signalr/negotiate?... not http://localhost:8080/negotiate?.... To fix this you can try the following before you call $.connection.hub.start:

$.connection.hub.url = http://localhost:8080/signalr;


Not sure if this question has been adequately answered, but I made the following changes to the sample provided by Microsoft:

public void Configuration(IAppBuilder app)
        {
            var config = new HubConfiguration();
            config.EnableJSONP = true;
            app.MapSignalR(config);
        }

And I added the following to the JS sample:

$.connection.hub.start({ jsonp: true }).done(function () {
    $('#sendmessage').click(function () {
        // Call the Send method on the hub.
        chat.server.send($('#displayname').val(), $('#message').val());
        // Clear text box and reset focus for next comment.
        $('#message').val('').focus();
    });
});

And now the Cross domain scripting is enabled. Hope this helps someone else, I was really puzzling with it for a while.


For Microsoft.Owin 2.x and above:

Add Microsoft.Owin.Cors package via NuGet by this command in Package Manager console:

PM> Install-Package Microsoft.Owin.Cors

and then using this package in Startup class file:

using Microsoft.Owin;
using Microsoft.Owin.Cors;

then change your source code like this:

// app.MapHubs(new HubConfiguration { EnableCrossDomain = true });
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();

Tags:

C#

Cors

Signalr