Jump to content

CORS policy/ importing local files

dwang040

For a course I'm taking, we use self made html pages to share our homework. Because of this, everything is supposed to be local files so web page urls look like "W://studentId/courseNum/index.html" and all the files follow the same format "W://studentId/courseNum/data.csv". For our next homework, we were given a series of latitudes and longitudes in data.csv and our goal is to display the markers with google maps implemented into our html page.

 

The question is, is there anyway for me to locally import it? Every method I've tried with ajax and jQuery all result in a "file has been blocked by CORS policy..." I get that it's a safety concern but does that mean there is absolutely zero way for me to somehow read my json or csv file into an array then locally? Am I just gonna have to copy and paste the content of the json/ csv into a large array in my javascript code? Idk, it just doesn't seem really ideal to have such a large array located locally in your javascript code.

//javascript file:
crimes = [ 
	{lat: a, lng: b},
    {lat: c, lng: d},
    {etc...}
]

function (){
	//read crimes
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

If I was you I'd just spin up a docker container with nginx and have them served to me. Only other way is to make the browser shut up, by disabling CORS, which I'm not sure if it's possible anymore. That's an extremely naughty (insecure) thing to do after all.

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, DevBlox said:

If I was you I'd just spin up a docker container with nginx and have them served to me. Only other way is to make the browser shut up, by disabling CORS, which I'm not sure if it's possible anymore. That's an extremely naughty (insecure) thing to do after all.

I see. Can't say I've heard or used nginx so I'll look into it in more detail. But quickly looking at it, the short answer seems to be I'm just gonna have to host the files somewhere or CORS won't leave me alone. 

Link to comment
Share on other sites

Link to post
Share on other sites

On 9/13/2019 at 11:28 PM, gabrielcarvfer said:

Try to spin a webserver in python. If you already have python installed, run "python -m http.server 80 --directory /path/to/serve".

Sorry for the late reply, but that's what I ended up doing to get around the issue xd. One question though, is there a cmd to flush the server cache or something? Sometimes I'll update the workspace folder (add/ change a png or update the code) and I'm still being fed old file stuff. Like if I changed an apple png to an orange, my web page would still display an apple image. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, gabrielcarvfer said:

For this server, I think the only way is to kill and restart the server. It's quite bothersome, but works just fine.
If you prefer, you can use the flask server (also in python) on debug mode, but it's quite more cumbersome if you only want to work on js/html.

I see, thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×