0

I was messing around with API's to gain familiarity with them, and I'm trying to display a list of object names from an API. But when I open my website, nothing is displayed and I get a CORS error. Here is my code:

const url = 'https://api.wynncraft.com/v2/ingredient/list';
const mainDiv = document.getElementById('container');
fetch(url)
.then((resp) => resp.json())
.then(function(data) {
 let ingredients = data.results;
 return ingredients.map(function(ingredient) {
     let li = createNode('li');
     let span = createNode('span');
     span.innerHTML = `${ingredient.name}`;
     append(li, span);
     append(ul, li);
 })
})
.catch(function(error) {
 console.log(error);
});

function createNode(element) {
 return document.createElement(element);
}

function append(parent, el) {
 return parent.appendChild(el);
}

And the associated HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Wynncraft API Test</title>
    </head>
    <body>
        <div id="container"></div>
        <script src="script.js"></script>
    </body>
</html>

Any help is greatly appreciated!

Error Code

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Jacob R.
  • 7
  • 3
  • 1
    Does this answer your question? [Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-receive-a-no-access-control-allow-origin-header-i) – onzinsky Oct 19 '21 at 17:47
  • 1
    And/or [any of these](/search?q=No+%27Access-Control-Allow-Origin%27+header+is+present+on+the+requested+resource) or [these](https://duckduckgo.com/?q=No+%27Access-Control-Allow-Origin%27+header+is+present+on+the+requested+resource+site%3Astackoverflow.com). Please search thoroughly before posting. More about searching [here](/help/searching). – T.J. Crowder Oct 19 '21 at 17:49
  • The API in question does not appear to be configured to allow for requests from browser-based JavaScript code, ostensibly for security reasons. Make the request using a server-sided language and call the code on your server to proxy the results. – esqew Oct 19 '21 at 17:51

0 Answers0