pulling data from external source and getting a No 'Access-Control-Allow-Origin' header is present on the requested resource

92 views Asked by At

I am trying to pull a JS file from an external source and get the following error:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

The file I am trying to pull is a JS file that has data in it. I have tried the following

var xhr = new XMLHttpRequest();
xhr.open("get", "https://s3-ap-southeast-2.amazonaws.com/[userid]/data.js", true);
xhr.onload = function(){  //instead of onreadystatechange
    //do something
    alert("here");
};
xhr.send(null);

I have also tried

var promise = $.ajax({
    url: "https://s3-ap-southeast-2.amazonaws.com/[userid]/data.js",
    method: 'GET'
});

promise.done(function (result) {
    alert("done");
}).fail(function(){
    alert("Data cannot be loaded at this time. Please ensure there are no ad blockers enabled.");
});

I have tried this with a JSON file and these works but it will not load in the JS file. Can anyone shed any light?

1

There are 1 answers

1
Ranjith S On

You need to add CORS headers in the response from where you are getting the data.No other way is possible.