0

I am trying to load a js from a different domain in my Ember app. However, I get the following error;

XMLHttpRequest cannot load https://external.main.com/my-client.js. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

Below is the code I use to call the JS (AJAX)

var promise = new Ember.RSVP.Promise(function(resolve, reject) {
    return $.ajax({
        url: requestUrl,
        type: 'POST', 
        dataType: 'JSON',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(postData),
        async: isAsync
    }).success(resolve).error(reject);
    })
    .catch(this.handleServerHTTPErrorCodes)
    .then(this.handleCommonServerErrors);
    return promise;

I also added the folowing config in environment.js

ENV.contentSecurityPolicy = {
  'connect-src': "'self' https://external.main.com"
}

How can I load an external JS in Ember ?

copenndthagen
  • 49,230
  • 102
  • 290
  • 442

1 Answers1

-2

Try:

ENV.contentSecurityPolicy = {
  'connect-src': "'self' http://localhost"
}

As a temporary workaround, try this in Chrome:

https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en

EDIT: Why are you trying to load a JS file through Ajax? Normally you would load it in, in the index.html file and then you can access its methods.

stijn.aerts
  • 6,026
  • 5
  • 30
  • 46