Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
84
rated 0 times [  86] [ 2]  / answers: 1 / hits: 38216  / 6 Years ago, sun, may 6, 2018, 12:00:00

I am trying to understand modules in JavaScript but when I try to write a very simple module I get CORS error.I am using bracket as my text editor and the odd part is when I'm using live preview in brackets the code works but when I normally open the .js file I get error.



index.html



<!DOCTYPE html>
<html>
<head>
<title> JS </title>
</head>
<body>
<script type=module>
import {add} from './libA.js';
console.log(add(10,20));
</script>
</body>
</html>


libA.js



export function add(a,b) {
return a+b ;
}


*I get this error ->



Access to Script at 'file:///F:/WEB%20DEV/JavaScript/libA.js' from origin `null` has been blocked by CORS policy: 
Invalid response.
Origin 'null' is therefore not allowed access.


(I even tried the latest version of chrome too)


More From » module

 Answers
1

Many browsers do not allow you to access files on the local filesystem with JavaScript (even if the HTML document is also on the local filesystem).



This extends to loading JavaScript modules.



Install a webserver and use that (with a URL like http://localhost/) for testing.


[#54499] Wednesday, May 2, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rhett

Total Points: 671
Total Questions: 100
Total Answers: 102

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
rhett questions
Thu, Oct 28, 21, 00:00, 3 Years ago
Mon, Aug 2, 21, 00:00, 3 Years ago
Fri, Dec 11, 20, 00:00, 3 Years ago
Fri, Aug 7, 20, 00:00, 4 Years ago
Thu, Mar 26, 20, 00:00, 4 Years ago
;