locked
please help with cross origin request error RRS feed

  • Question

  • User1642115476 posted

    Hello,

    I'm trying to solve a cross origin request bug that's reported in the Chrome console like this:

    XMLHttpRequest cannot load file:///C:/Users/junkm/Desktop/temp/Bowtie/GetProject?projectId=186. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. jquery-2.2.4.min.js:4

    It's a rather complicated setup so bear with me.

    It starts with a simple htlm page running on my desktop. The page has a reference:

    <script src="http://ec2-54-236-63-55.compute-1.amazonaws.com/RiskAliveAPI/scripts/bowtie/bowtie.js"></script>

    ^ This is in our web API.

    The page also has a script:

    <script>
    var myBowtieDiagram = new acm.ra.BowtieDiagram("bowtieDiagram");
    myBowtieDiagram.setProjectId(186);
    </script>

    acm.ra.BowtieDiagram and myBowtieDiagram.setProjectId() are defined in bowtie.js (the file in the reference).

    myBowtieDiagram.setProjectId() looks like this:

    setProjectId(projectId) {
    if (this.BowtieSelectList !== null) {
    this.BowtieSelectList.setProjectId(projectId);
    }
    }



    BowtieSelectList.setProjectId() looks like this:

    setProjectId(projectId) {
    acm.ra.BowtieAPI.GetProjectBowties(projectId, this.loadSelectList.bind(this));
    }



    And acm.ra.BowtieAPI.GetProjectBowties() in turn looks like this:

    static GetProjectBowties(projectId, onSuccess, onFail) {
    $.get({
    url: 'Bowtie/GetProject',
    data: { projectId: projectId },
    traditional: true
    })
    .done(function (data) {
    if (onSuccess)
    onSuccess(data);
    })
    .fail(function (jqXhr, textStatus, errorThrown) {
    if (onFail)
    onFail(jqXhr, textStatus, errorThrown);
    console.log('GetProjectBowties Error: ' + textStatus);
    console.log(errorThrown);
    });
    }



    The cross origin request error that I get happens in the .fail(...) of the ajax call.

    What do I have to do to get the cross origin error to go away?

    Wednesday, August 2, 2017 5:13 PM

All replies

  • User475983607 posted

    If you are trying to get a web server to open a file on a user's local drive.   It simply will not work due to browser security.  

    IF you opened an HTML page from your local hard drive, then the following line is trying to make an HTTP call to your local drive which will not work either.

    url: 'Bowtie/GetProject',

    What problem are you trying to solve?

    Wednesday, August 2, 2017 5:31 PM
  • User-271186128 posted

    Hi gib9898_00,

    XMLHttpRequest cannot load file:///C:/Users/junkm/Desktop/temp/Bowtie/GetProject?projectId=186. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. jquery-2.2.4.min.js:4

    As the error message said, the request only supported for Http protocol schemes, instead of "file://".

    So, you need to actually run a webserver, and make the get request to a URI on that server. Then the url should like this: "http://example.com/GetProject?projectId=186" or "http://localhost/resources/templates/GetProject?projectId=186"

    Here are some threads with the similar problem, you could refer to them:

    https://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local

    https://stackoverflow.com/questions/20041656/xmlhttprequest-cannot-load-file-cross-origin-requests-are-only-supported-for-ht

    Best regards,
    Dillion

    Thursday, August 3, 2017 7:55 AM