C
C
cataclysm882021-07-07 19:45:07
JavaScript
cataclysm88, 2021-07-07 19:45:07

What to do if CORS error?

In general, there is such code:
HTML:

<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset='UTF-8'>
  <title>Test</title>
  <link href="style.css" rel='stylesheet' type='text/css'>
  <script type='module' src='app.js'></script>
</head>
<body>
  <h1 id='money'>0</h1>
  <button type='button' id='farm'>Click me!</button>
</body>
</html>


CSS:
#money {
  color:crimson;
  text-align: center;
}

#farm {
  color:darkblue;
  background-color:firebrick;
  width: 100%;
  height: 50px;
  font-size: 25px;
}

body {
  background-color: gray;
}


Javascript:
let money = document.getElementById('money')
const farm = document.getElementById('farm')

function addMoney() {
  money.textContent++
}

farm.addEventListener('click', addMoney)


The essence of the code is that when you click on the Click me button, the value of `money` is incremented. However, when the page is launched, without having time to do anything, an error immediately flies to the console:
Access to script at ' file:///C:/Users/user/Desktop/JS/app.js ' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
---------------------
How can I fix this? Please need help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lightseeker, 2021-07-07
@lightseeker

Well, first of all, remove type=module, you don't need it.
And secondly, include the script not in the head, but before the closing tag </body>.
You must be making a mistake here

Cannot read property addEventListener from undefinded

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question