Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Web] Failed to complete negotiation with the server: XMLHttpRequest error. #27

Closed
AmbujaAK opened this issue Dec 3, 2020 · 7 comments
Labels
bug Something isn't working

Comments

@AmbujaAK
Copy link

AmbujaAK commented Dec 3, 2020

version : signalr_core: ^1.0.5
code :

    final connection = HubConnectionBuilder()
        .withUrl(
            "$kBaseUrl/serverpush",
            HttpConnectionOptions(
              logging: (level, message) => print('$level: $message'),
            ))
        .build();

    await connection.start();

    connection.on('FlutterRecMsgFromHub', (message) {
      print(message.toString());
    });

Issues :
Restarted application in 670ms.
LogLevel.debug: Starting HubConnection.
LogLevel.debug: Starting connection with transfer format 'TransferFormat.text'.
LogLevel.debug: Sending negotiation request: https://p4infrdevsignalrwebapp01.azurewebsites.net/serverpush/negotiate.
LogLevel.error: Failed to complete negotiation with the server: XMLHttpRequest error.
LogLevel.error: Failed to start the connection: XMLHttpRequest error.
LogLevel.debug: HubConnection failed to start successfully because of error '{XMLHttpRequest error..toString}'.
Error: XMLHttpRequest error.
at Object.createErrorWithStack (http://localhost:63192/dart_sdk.js:4362:12)
at Object._rethrow (http://localhost:63192/dart_sdk.js:38245:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:63192/dart_sdk.js:38239:13)
at Object._microtaskLoop (http://localhost:63192/dart_sdk.js:38071:13)
at _startMicrotaskLoop (http://localhost:63192/dart_sdk.js:38077:13)
at http://localhost:63192/dart_sdk.js:33574:9

@jamiewest jamiewest added the bug Something isn't working label Dec 4, 2020
@AdrianKlm
Copy link

I have the same problem. Any information on when it will be fixed?

@Jddl
Copy link
Contributor

Jddl commented Mar 11, 2021

            services.AddCors(op =>
            {
                op.AddPolicy("SignalRCors", set =>
                {
                    set.SetIsOriginAllowed(origin => true)
                       .AllowAnyHeader()
                       .AllowAnyMethod()
                       .AllowCredentials();
                });
            });

@laimonas
Copy link

@AmbujaAK maybe try to look there
#24 (comment)

@agwanyaseen
Copy link

Same issue, these plugin not working on flutter web , Any Idea till when it will going to be fixed?

@milodude
Copy link

The XMLHttpRequest error is due to Cors policy issue. I managed to solve it by doing the next steps in a C# application.

1- Define in your startup.cs file a new porperty ie:
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

2-At your configure method in the same Startup.cs file add the next line to add Cors
services.AddCors(options => { options.AddPolicy(MyAllowSpecificOrigins, policy => { policy.WithOrigins("http://localhost:58944") .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); });
Where WithOrigins must be filled with the UI host url you are trying to send the message from.

3- Use Cors at your Configure method in the same Startup.cs file
app.UseCors(MyAllowSpecificOrigins); app.UseRouting();
It must be defined before UseRouting method.

Please give it a shot and let everyone know if this worked out for you.

@petsve
Copy link

petsve commented Oct 5, 2021

Allowing Cors solved it for me too. But the port could change between runs so I made the server code a bit more generic.
In .NET you could use the following:

services.AddCors(options => { options.AddPolicy(MyAllowSpecificOrigins, builder => builder.SetIsOriginAllowed(origin => new Uri(origin).IsLoopback) ); });

@Codzaa
Copy link

Codzaa commented Dec 19, 2021

@jamiewest Hi !

I bumped into this same error but I found a different solution that does not involve configuring 'CORS'.

final connection = HubConnectionBuilder() .withUrl( 'http://localhost:9670/hub', HttpConnectionOptions( skipNegotiation: true, transport: HttpTransportType.webSockets, logging: (level, message) => print(message), )) .build();

You can simply set skipNegotiation to true, but when reading the docs on the microsoft site (https://docs.microsoft.com/en-us/javascript/api/%40aspnet/signalr/ihttpconnectionoptions?view=signalr-js-latest#skipnegotiation) , it says that you have to be using Web Sockets for this to work.

By the way what are the downsides to setting skipNegotiation to true, any security issues or something?

Environment
Flutter 2.8.0
.Net 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants