0

I have an application that uses the Http package on a browser. I'm using Dart 2.4.1 with angular_components. WebStorm Ide 2019.2 I'm using a method similar to the following:

  final String url = 'http://127.0.0.1:8080/api/check';

    String postData =
        '{"email":"abc@dart.com"}';

    final client = BrowserClient();
    Response response;
    try {

        response = await client.post(url,
            headers: {
              'Content-Type': 'application/json',
            },
            body: postData);

    } on ClientException catch (e) {
      print(e);
    } catch (e) {
      print(e);
    } finally {
      client.close();
    }

But I get a ClientException with text "XMLHttpRequest error." and stack:

Error: XMLHttpRequest error.
    dart:sdk_internal 41864:30                                                                    get current
package:http/src/packages/http/src/browser_client.dart 84:22                                  <fn>
package:stack_trace/src/packages/stack_trace/src/stack_zone_specification.dart 129:26         <fn>
package:stack_trace/src/packages/stack_trace/src/stack_zone_specification.dart 209:15         [_run]
package:stack_trace/src/packages/stack_trace/src/stack_zone_specification.dart 129:14         <fn>
package:angular/src/core/change_detection/packages/angular/src/core/zone/ng_zone.dart 144:18  <fn>
dart:sdk_internal 28355:14                                                                    runUnary
package:angular/src/core/change_detection/packages/angular/src/core/zone/ng_zone.dart 141:18  [_runUnary]
dart:sdk_internal 99587:96                                                                    <fn>
package:stack_trace/src/packages/stack_trace/src/stack_zone_specification.dart 129:26         <fn>
package:stack_trace/src/packages/stack_trace/src/stack_zone_specification.dart 209:15         [_run]
package:stack_trace/src/packages/stack_trace/src/stack_zone_specification.dart 129:14         <fn>
package:angular/src/core/change_detection/packages/angular/src/core/zone/ng_zone.dart 144:18  <fn>
dart:sdk_internal 28355:14                                                                    runUnary
package:angular/src/core/change_detection/packages/angular/src/core/zone/ng_zone.dart 141:18  [_runUnary]
dart:sdk_internal 28515:34                                                                    <fn>


    at Object.dart.createErrorWithStack (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:4617:12)
    at onError (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:22719:44)
    at Object.dart._checkAndCall (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:4787:16)
    at Object.dart.dcall (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:4812:17)
    at http://localhost:53322/packages/stack_trace/src/chain.ddc.js:850:127
    at src__stack_zone_specification.StackZoneSpecification.new.[_run] (http://localhost:53322/packages/stack_trace/src/chain.ddc.js:893:16)
    at http://localhost:53322/packages/stack_trace/src/chain.ddc.js:850:95
    at http://localhost:53322/packages/angular/src/core/change_detection/change_detection.ddc.js:2435:18
    at async._rootRunBinary (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:28744:56)
    at async._ZoneDelegate.new.runBinary (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:28361:14)
    at src__core__zone__ng_zone.NgZone.new.[_runBinary] (http://localhost:53322/packages/angular/src/core/change_detection/change_detection.ddc.js:2432:21)
    at async._CustomZone.new.runBinary (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:28569:14)
    at _FutureListener.then.handleError (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:24650:52)
    at handleError (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:25118:51)
    at Function._propagateToListeners (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:25141:17)
    at _Future.new.[_completeError] (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:25004:23)
    at http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:25037:31
    at http://localhost:53322/packages/angular/src/core/change_detection/change_detection.ddc.js:2401:11
    at src__stack_zone_specification.StackZoneSpecification.new.[_run] (http://localhost:53322/packages/stack_trace/src/chain.ddc.js:893:16)
    at http://localhost:53322/packages/stack_trace/src/chain.ddc.js:840:71
    at http://localhost:53322/packages/angular/src/core/change_detection/change_detection.ddc.js:2415:18
    at async._rootRun (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:28729:14)
    at async._ZoneDelegate.new.run (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:28349:14)
    at src__core__zone__ng_zone.NgZone.new.[_run] (http://localhost:53322/packages/angular/src/core/change_detection/change_detection.ddc.js:2412:21)
    at async._CustomZone.new.run (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:28555:14)
    at async._CustomZone.new.runGuarded (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:28472:14)
    at async._AsyncCallbackEntry.new.callback (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:28511:33)
    at Object.async._microtaskLoop (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:25352:13)
    at async._startMicrotaskLoop (http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:25358:13)
    at http://localhost:53322/packages/build_web_compilers/src/dev_compiler/dart_sdk.js:25433:9

The strange thing is that I have been working for two hours when suddenly it stopped to work.

in the console I get:

Access to XMLHttpRequest at 'http://127.0.0.1:8080/api/check' from origin 'http://localhost:53322' has been blocked by CORS policy: Request header field x-ijt is not allowed by Access-Control-Allow-Headers in preflight response.

I tryed to use the same method inside a commandLine program (Using IOClient) and everything works fine. I tried to roll back to webdev 2.3.0 (when I checked I was using 2.4.0) but nothing changed.

More, in the console I see also errors in retrieving the roboto font with a message similar to the one of my error.

Access to font at 'https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4mxK.woff2' from origin 'http://localhost:53322' has been blocked by CORS policy: Request header field x-ijt is not allowed by Access-Control-Allow-Headers in preflight response.
index.html:1GET https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4mxK.woff2 net::ERR_FAILED

Please, note that everything was working fine and I did not change the configuration of my app today.

Any Hint?

Update 1: googling a little I found this page: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000715304-Problem-with-Chrome-plugin-and-CORS

It seem that he IDE adds the "x-ijt" header unless you set the "Allow unsigned requests" in Settings | Build, Execution, Deployment | Debugger.

I tried and this way it works. Now I would like to understand why this happened during my work session without any change to the configuration. Anyone can help?

J F
  • 1,058
  • 2
  • 10
  • 21
  • You your problem seems related to these posts, check if any of the solution works for you: https://stackoverflow.com/q/32500073/8745788 https://stackoverflow.com/q/42061727/8745788 – Mattia Aug 09 '19 at 14:35
  • Thanks, probably it is an IDE problem as I pointed out in my post update. – J F Aug 09 '19 at 15:15

0 Answers0