XMLHttpRequest Cross origin requests In Dart with IntelliJ

Issue

I’m loading a json file relatively using dart’s html library’s HttpRequest class. In the Dart editor it’s working fine. Using the dart plugin for intelliJ with JetBrains chrome extension I receive…

XMLHttpRequest cannot load file:///C:/project/web/path/to/my/file.json. Cross origin requests are only supported for HTTP.  :0
Exception: Error: NETWORK_ERR: XMLHttpRequest Exception 101
Stack Trace: #0      HttpRequest.send (file:///E:/b/build/slave/dartium-win-full-trunk/build/src/build/Release/obj/global_intermediate/webkit/bindings/dart/dart/html/HttpRequest.dart:33:3)

I’m passing a relative url, when inspecting the URL I see its:
“path/to/my/file.json”

Here’s the bare bones test.

import 'dart:html';

main(){
  Loader loader = new Loader();
  loader.load("someFile.json");
}

class Loader {
  load(String url) {
    var request = new HttpRequest();
    request.open('GET', url, true);
    request.send();
  }
}

Solution

I am not sure why it works in one context and not another, but basically cannot just go GET a file if you are not using http. See these for details on why this is the case. I’ve never used IntelliJ, so I cannot speak to the specifics of that context, unfortunately.

http://stackoverflow.com/questions/8449716/cross-origin-requests-are-only-supported-for-http-but-its-not-cross-domain
http://stackoverflow.com/questions/3102819/chrome-disable-same-origin-policy
http://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-but-im-loading-a-co

Answered By – Shailen Tuli

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.