0

I am trying to retrieve data from a premium API on Quandl.com with the below JavaScript code, and display the results in my browser. Both the JS file and the HTML file are on my local computer, but the following problem appears also when incorporating the code on my webpage:

The first, public API URL (the one deactivated in the comment field) works fine and returns the expected data. The second, premium API URL leads to the following error in my Google Chrome console:

Access to XMLHttpRequest at 'https://www.quandl.com/api/v3/datatables/SHARADAR/SEP.json?ticker=AAPL&api_key=xxxxx' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

What is possibly causing this issue, and is there a way to change the javascript code to solve this problem?

JavaScript (quandldata.js):

/*! var url = "https://www.quandl.com/api/v3/datasets/CURRFX/JPYSEK.json" */

var url = "https://www.quandl.com/api/v3/datatables/SHARADAR/SEP.json?ticker=AAPL&api_key=xxxxxxxx"

function setup() {
noCanvas();
loadJSON(url, gotData);
}

function gotData(data) {
    var quandl = data.dataset.data
    createElement('h1', data.dataset.column_names);
    for (var i = 0; i < 5; i++){
        createP(quandl[i])
    }
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head> 
    <meta charset="utf-8" />
    <title>QuandlData</title>
    <link href="quandldata.css" rel="stylesheet" type="text/css" />
</head>

<body>
 <div id="root"></div>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.20/p5.min.js"></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.20/addons/p5.dom.min.js"></script> 
 <script src="quandldata.js"></script>
</body>
</html>
F.Marks
  • 159
  • 2
  • 12
  • 1
    _"What is possibly causing this issue"_ -> https://en.wikipedia.org/wiki/Cross-origin_resource_sharing, _"and is there a way to change the javascript code to solve this problem"_ -> No – Andreas Feb 23 '19 at 06:41
  • Thanks Andreas. This is a premium datafeed and eventually I want to use the code to display the data on a website, as others are doing. I expect that there must be some way to alter the code, or other parameters, and make it work. Any further suggestions? – F.Marks Feb 23 '19 at 06:51
  • 1
    The owner of the API doesn't wan't you to access the data via JavaScript. You cannot change that. You will have to use server-side code to get the feed. – Andreas Feb 23 '19 at 06:53
  • Thank you Andreas and Slideshowbarker, that was very valuable information. I made it work by using the suggested other post Slideshowbarker made me aware of, appreciate it!! (https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API) – F.Marks Feb 23 '19 at 17:36

0 Answers0