I’m trying to write to my (html/js/css) repl’s db as part of it’s website using:
let xhr = new XMLHttpRequest();
xhr.open("POST", <DB URL>, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(<DATA>));
however I am being blocked by CORS, with the error:
Access to XMLHttpRequest at from origin has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
I’ve seen some people suggesting using cors-anywhere but I’d rather fix the problem the “intended” way, by setting the correct header on the server. However I don’t know how to do this, or even if it is possible to.
I’ve tried making a similar request using curl, which worked fine, and I’ve tried querrying a database from a different repl instead of the host repl, which gave me the same error. So I think the problem is cuased by making a post request from a repl-hosted website.
If there isn’t a solution to this I could made my program as a desktop app, but this complicates other things, so I’d like to keep it a website if possible.