Dio: Flutter Web - XMLHttpRequest error

Created on 21 Apr 2020  路  14Comments  路  Source: flutterchina/dio

| Info | Value | |
| ------------------------------ | ----------------------------------------------------- | ---- |
| Platform Name | flutter web | |
| Platform Version | 1.12.13+hotfix.9 | |
| Dio Version | 3.0.9 | |
| Repro with our demo prj | e.g. does it happen with our demo project? |

Future<Response> get({
    String innerPath = "",
    CancelToken cancelToken,
    Map<String, String> queryParameters = const {},
  }) async {
    try {
      final response = await _client.get(
        "/$_path/$innerPath",
        cancelToken: cancelToken,
        queryParameters: queryParameters,
      );
      throwIfNoSuccess(response);
      return response;
    } catch (e) {
      throw e;
    }
  }

It works on Android and iOS side. I get this error on the flutter-web.

Request path : [GET] 'A url to make get request'
Request data : null
Error message : XMLHttpRequest error.
DioError [DioErrorType.RESPONSE]: XMLHttpRequest error.

stale

Most helpful comment

Has anyone found what the fix for this might be?

All 14 comments

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

the same for me

I had the problem, I add this in my site (index.php) and it worked.

header("Access-Control-Allow-Origin: *");

No, still same problem.

@cankarabag Did you check the browser console for errors? I searched the dio source code for this error, and I found this:

// Unfortunately, the underlying XMLHttpRequest API doesn't expose any
// specific information about the error itself.
completer.completeError(
  ClientException('XMLHttpRequest error.', request.url),
  StackTrace.current);

So dio itself can't print a more helpful error message, but it will get logged in the browser console.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

Has anyone found what the fix for this might be?

Has anyone found solution?

Hey, Dio does not work on the web may because CORS which not appear in APP. I fixed the problem by adding this in response's header.
"Access-Control-Allow-Origin": "*", // Required for CORS support to work "Access-Control-Allow-Credentials": true, // Required for cookies, authorization headers with HTTPS "Access-Control-Allow-Headers": "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token", "Access-Control-Allow-Methods": "GET, POST, OPTIONS"

Hey, Dio does not work on the web may because CORS which not appear in APP. I fixed the problem by adding this in response's header.

  "Access-Control-Allow-Credentials": true, // Required for cookies, authorization headers with HTTPS
  "Access-Control-Allow-Headers": "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
  "Access-Control-Allow-Methods": "GET, POST, OPTIONS"

Does this means dont use dio?

I have the exact same issue, works perfectly fine on mac and ios:

Error: DioError [DioErrorType.RESPONSE]: XMLHttpRequest error. at Object.throw_ [as throw] (http://localhost:55644/dart_sdk.js:4338:11) at http://localhost:55644/packages/dio/src/entry/dio_for_browser.dart.lib.js:357:31 at _RootZone.runUnary (http://localhost:55644/dart_sdk.js:37756:58) at _FutureListener.then.handleValue (http://localhost:55644/dart_sdk.js:32717:29) at handleValueCallback (http://localhost:55644/dart_sdk.js:33265:49) at Function._propagateToListeners (http://localhost:55644/dart_sdk.js:33303:17) at _Future.new.[_completeWithValue] (http://localhost:55644/dart_sdk.js:33145:23) at async._AsyncCallbackEntry.new.callback (http://localhost:55644/dart_sdk.js:33168:35) at Object._microtaskLoop (http://localhost:55644/dart_sdk.js:38017:13) at _startMicrotaskLoop (http://localhost:55644/dart_sdk.js:38023:13) at http://localhost:55644/dart_sdk.js:33520:9

same issue

Encountering same issue

same issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vico-aguado picture vico-aguado  路  6Comments

keluokeda picture keluokeda  路  3Comments

Dmitry-N-Medvedev picture Dmitry-N-Medvedev  路  3Comments

rrifafauzikomara picture rrifafauzikomara  路  5Comments

neeraj87 picture neeraj87  路  4Comments