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 });
});
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With