XMLHttpRequest blocked by CORS policy | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

25th Mar 2019, 4:41 AM
Gordon
Gordon - avatar
14 Answers
+ 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
25th Mar 2019, 6:35 AM
Calviղ
Calviղ - avatar
+ 3
Gordon you have to use http protocol instead of file protocol. Also you can try to include crossorigin ="anonymous"
31st Oct 2019, 12:49 PM
r8w9
r8w9 - avatar
+ 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
31st Oct 2019, 2:11 PM
r8w9
r8w9 - avatar
+ 2
r8w9 oh i see, thanks
31st Oct 2019, 2:25 PM
Gordon
Gordon - avatar
+ 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
25th Mar 2019, 4:57 AM
Sudarshan Rai
Sudarshan Rai - avatar
+ 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?
25th Mar 2019, 5:02 AM
Gordon
Gordon - avatar
+ 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.
25th Mar 2019, 5:57 AM
Calviղ
Calviղ - avatar
+ 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
25th Mar 2019, 6:44 AM
Calviղ
Calviղ - avatar
+ 1
You always can check from your current ip address. https://www.whatismyip.com By opening the address from browser.
25th Mar 2019, 6:51 AM
Calviղ
Calviղ - avatar
+ 1
Coder understand, current trend is to print logs in html
31st Oct 2019, 1:15 PM
Gordon
Gordon - avatar
0
Calviղ but it prompts this error when I am running the file locally.
25th Mar 2019, 6:03 AM
Gordon
Gordon - avatar
0
Do you have locals server?
25th Mar 2019, 6:32 AM
Calviղ
Calviղ - avatar
0
Calviղ if I configure this way, means someone outside can also access these files on my computer? 🤔
25th Mar 2019, 6:42 AM
Gordon
Gordon - avatar
0
r8w9 oh i can use http protocol to access local files?
31st Oct 2019, 12:56 PM
Gordon
Gordon - avatar