Easy solution for whom using VS Code
I've been getting this error for a while. Most of the answers works. But I found a different solution. If you don't want to deal with node.js
or any other solution in here and you are working with an HTML file (calling functions from another js file or fetch json api's) try to use Live Server extension.
It allows you to open a live server easily. And because of it creates localhost
server, the problem is resolving. You can simply start the localhost
by open a HTML file and right-click on the editor and click on Open with Live Server
.
It basically load the files using http://localhost/index.html
instead of using file://...
.
EDIT
It is not necessary to have a .html
file. You can start the Live Server with shortcuts.
Hit
(alt+L, alt+O)
to Open the Server and(alt+L, alt+C)
to Stop the server. [On MAC,cmd+L, cmd+O
andcmd+L, cmd+C
]
Hope it will help someone :)