I got this error while trying to launch an html page using a javascript file which imported a function from another file:
Access to script at 'file:///C:/Users/bla/Desktop/stuff/practice/jsPractice/funcexecute.js'
from origin 'null' has been blocked by CORS policy:
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
Failed to load resource: net::ERR_FAILED
Here's the html code:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Page Title</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<body>
<script type = 'module' src='funcexecute.js'></script>
</body>
</html>
js file which was called from the html: (funcexecute.js)
import sumup from '/funcfile.js';
console.log(sumup(1,2));
Imported module: (funcfile.js)
function sumup(num1, num2){
return num1+num2;
}
export default sumup;
How can i fix this? (im using vscode)
From Origin 'null' Has Been Blocked By CORS Policy Error. To fix this error is also easy, what you need to do is to create a local web server, and then upload the Html and JS file to the webserver.
Access to XMLHttpRequest at 'http://127.0.0.1:5000/predict_home_price' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. You need to add CORS headers to the response coming back from wherever url is pointing to.
You cannot use a script file as a module without using a server. Take a look here
Here are some of the options
http-server .
from your project directory)In short cannot use type="module"
with file protocol
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With