+ 5
XMLHttpRequest blocked by CORS policy
I want to create an application which interacts with a local file which acts as a database of JSON object. I followed Stackoverflow : https://code.sololearn.com/WjcNo9zLK55l/?ref=app But it logs error message: Access to XMLHttpRequest at 'file:///C:/Users/Gordon/Desktop/testing.txt' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. How to solve this? But if I set some low protection level, means malicious attempt from Internet also allowed?
15 Réponses
+ 5
Run local server to access the web file.
If you do not have local server installed.
Use cmd prompt to run
npm install http-server -g
to install a local server
(You need to have nodejs installed)
Then run
http-server -p8000
from your working folder.
Open browser with address http://localhost:8000
+ 3
Gordon you have to use http protocol instead of file protocol. Also you can try to include crossorigin ="anonymous"
+ 2
Gordon i don't think so :)
-http protocol == web hosting or you can host it on your local machine using xampp
-file transfer protocol == your local computer that runs your index.html file => here cors won't let you continue
+ 2
r8w9 oh i see, thanks
+ 1
//You can use this API, it enables cross-origin requests to anywhere.
https://cors-anywhere.herokuapp.com
://Deom
https://github.com/Rob--W/cors-anywhere
+ 1
🤔
But I want to process sensitive information of my company, and so I don't want the data to be uploaded to somewhere. Is there any way?
+ 1
Sololearn playground is running from codes.sololearn domain, so you have cors issue.
If you create local html/js file, it should be working fine.
+ 1
Dy defualt, your windows blocks the web access from outside, unless you configure it to allow your ip to expose to web through port 80
+ 1
You always can check from your current ip address.
https://www.whatismyip.com
By opening the address from browser.
+ 1
Coder understand, current trend is to print logs in html
0
Calviղ but it prompts this error when I am running the file locally.
0
Do you have locals server?
0
Calviղ if I configure this way, means someone outside can also access these files on my computer? 🤔
0
r8w9 oh i can use http protocol to access local files?