“Access to script at ‘file:///C:/Users/Claudia/Desktop/expenses-chart-component-main/expenses-chart-component-main.js’ from origin ‘null’ has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, isolated-app, brave, https, chrome-untrusted, data, chrome-extension, chrome.”
import data from 'https://www.jsonkeeper.com/b/QI82' assert { type: "json" };
from what I read online the json data needs to be on a hosted/server site , can’t be local?
I first tried a link to a raw github json file , that didn’t work.
Then I tried using https://www.jsonkeeper.com/
didn’t work either. I’m lost as what to do about this error.
You can use a CORS proxy but that isn’t meant for production.
Create a simple Express server on something like Replit/Glitch or some other free hosting site. Add the cors package and serve the JSON yourself. Then import the JSON from that endpoint.
Thanks. It tried the Cors Proxy, but can’t get it to work? It should be fine as my code is just a practice project , so not worried about production.
I tried this import statement:
import data from 'https://corsproxy.io/?https://raw.githubusercontent.com/cmb347827/expenses-chart-component-main/main/data.json?token=GHSAT0AAAAAACCV2CADPZLX4FDZXOGL7NBYZDCTKEA' assert{ type:'json'};
tried this as well
import data from 'https://corsproxy.io/?https://www.jsonkeeper.com/b/QI82' assert { type: "json" };
and still get the same error ?
Hope you can help.
Thanks
It is hopeless. I will not be able to figure this out on my own, as it seems like back-end develpment. I have only focused on front-end and plan to stick to front-end.
Is there a way a noob like me can solve this ?
Thanks, I managed to get it to work with async / await .
Now I’m trying to export/import a JS function from one JS file to another (both local on my laptop). Everywhere I look online I get the feeling this is only available in node.js (backend) ? Because I get a syntaxerrror when I try to use export in one file.
Is this even possible to do local ?
Thanks
Acess to script at ‘file:///C:/Users/Melissa/Desktop/codementor/junior/expenses-chart-component-main/expenses-chart-component-main.js’ from origin ‘null’ has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, isolated-app, brave, https, chrome-untrusted, data, chrome-extension, chrome.
You need to be running this on a local server. If you are using modules you cannot load them using the file protocol, it has to be via (eg) http, as the error message states. You get this error if you just open an html file in a browser and it tries to load a JS module.
It’s absolutely not backend development, this is just a standard thing you need to understand because it directly affects your ability to develop stuff.
As said, need to understand exactly what you’re doing but you really need to be using a local development server to develop stuff and it looks very much like you aren’t if you’re trying to load modules using file://
Just to be specific re this absolutely not being backend, it is helpful to learn what a browser is doing, what HTTP is, what CORS is, etc. These are core things re frontend. The browser in this case is preventing you opening the file because there are sensible security protocols that prevent you doing this: it is important to understand why they are there
I already understood files needed to be on a remote server, i had tried earlier with Github repo files , but got the same errors.
How do I do this using a Github repo ?
Thanks
I mean , if I have two JS files in a repo, and want to link to one of them in an import statement , then it should work as these files are not local? One is my expenses-chart-component-main.js file and the other addCarets.js
Do I need to host the JS file I want to export on some other site then github?
Github is not a CDN. GitHub is a place to store versions of a code for development & version control. The thing you’re trying to link isn’t a JS file. GitHub sets the content type as “text/plain” to ensure you can’t do what you’re trying to do. It has done this for about 10 years to combat hotlinking. Again, to stress, it’s not a file serving provider.
The question here is why can you not just put the file in your project instead of trying to dynamically link it from somewhere? Otherwise, yes, you need to put the file somewhere on a server you can access if you want to access it remotely.
Yep I can see it says that. It’s years out of date now and I’m not sure why it hasn’t been updated at all because it’s just flat out wrong (when it was first coming in there wasn’t support in Node, the Node support has come after browser support) – be careful about just going off a single article (MDN should normally be first place to check)
And off course if I do the same in my project I get the stupid CORS error again.
It’s okay when I use https://corsproxy.io/?https://www.jsonkeeper.com/b/6P7P instead .
I understand why, but why is it possible the other way (local ) in the tutorial ?
I don’t care much for MDN myself.