7

I recently upgraded a project from SignalR 2.0.0-beta1 to 2.0.0-rc1. I understand that in RC1, configuration of support for cross domain requests changed. I've updated my project to use the new syntax however I'm now getting the following error when attempting to communicate with my hub:

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 client site is running at http://localhost:7176 and the hub is listening via a console application at http://localhost:8080. Am I missing something here? Cross domain requests were working before I upgraded to RC1.

CONSOLE APP ENTRY POINT

static void Main(string[] args)
{
    var chatServer = new ChatServer();
    string endpoint = "http://localhost:8080";

    chatServer.Start(endpoint);

    Console.WriteLine("Chat server listening at {0}...", endpoint);
    Console.ReadLine();
}

CHATSERVER CLASS

public class ChatServer
{
    public IDisposable Start(string url)
    {
        return WebApp.Start<Startup>(url);
    }
}

STARTUP CONFIGURATION

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Map("/signalr", map =>
        {
            map.UseCors(CorsOptions.AllowAll);
            map.RunSignalR(new HubConfiguration { EnableJSONP = true });
        });
    }
}
Scott
  • 13,735
  • 20
  • 94
  • 152

4 Answers4

8

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;

halter73
  • 15,059
  • 3
  • 49
  • 60
  • I knew it was something stupid. I got so focused on the fact that I had changed the approach to cross domain configuration I didn't realize I had inadvertently changed something else as well. Thanks. – Scott Aug 27 '13 at 18:40
  • So what's the best way to handle the cross domain in signalR when there are multiple environments involved for the server side e.g. dev, uat and production? I mean is there any way apart from manually changing the hub connection in the each generated hub file? – dragonfly02 Jan 31 '17 at 22:07
8

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.

Corstian Boerman
  • 848
  • 14
  • 32
user2760821
  • 109
  • 4
  • Looked all over for answers, and luckily found yours. Thanks! – Trevor de Koekkoek Apr 13 '14 at 14:31
  • { jsonp: true } Upvote for this. Does it foce to use json? or it just allows it to be used? – Tauseef Jun 16 '14 at 18:44
  • @Tauseef - Adding the JSONP flag does enforce the use of JSON as the message transport, as JSONP is a way of passing JSON data across different domains, please see: [link]http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about[/link] – user2760821 Jun 18 '14 at 06:05
5

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();
halfer
  • 19,824
  • 17
  • 99
  • 186
Sayed Abolfazl Fatemi
  • 3,678
  • 3
  • 36
  • 48
2

I think the best way to handle Cross Domain is documented here CrossDomain Signal R

Bitmask
  • 918
  • 2
  • 11
  • 22