andrewthong / covid19tracker-api

API backend for COVID-19 Tracker Canada project
https://api.covid19tracker.ca/
MIT License
37 stars 5 forks source link

No 'Access-Control-Allow-Origin' header is present on the requested resource. #33

Closed avrobullet closed 4 years ago

avrobullet commented 4 years ago

Request access to __https://api.covid19tracker.ca/reports?fill_dates&stat=&date&after&before__ is blocked by CORS policy.

andrewthong commented 4 years ago

You can use a proxy to make requests. But I did consider wildcard * for CORS ~ how are you planning on using this API?

avrobullet commented 4 years ago

I am currently looking for another COVID19 API that has frequent updates on provincial Canadian statistics, and I found your API's documentation on Postman. I want to use your API for my Canadian COVID19 personal project to replace another API that hasn't updated since May.

As for the accessing the API, I was creating a simple request through XMLHttpRequest() as follows that had worked with the previous API I was using: var request = new XMLHttpRequest() request.open('GET', covid19_urls[0], true) request.onload = function() {...//JSON parsing code here...}

andrewthong commented 4 years ago

The request won't work as the browser enforces CORS whereas Postman does not. You can get around this by adding a proxy script on your server (looks like you're running PHP/WordPress) — your request would go to the script which in turn is fetching data from the API.

More info on CORS: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

avrobullet commented 4 years ago

Thanks! I'll give it a shot this weekend.

geball commented 3 years ago

Did you have any luck, @avrobullet? I'm trying to get around the CORS problem on a Next.js project but am having no luck.