zzorba / marvelsdb

MarvelsDB - a deck builders for Marvel Champions - The Card Game
52 stars 28 forks source link

Accessing API through javascript #193

Closed seastan closed 1 year ago

seastan commented 1 year ago

I'm having trouble accessing the MarvelCDB API through a JavaScript application. I've built a codesandbox that demonstrates the issue here: https://codesandbox.io/s/fetch-data-from-url-forked-h54pzq?file=/src/App.js

The error in the console says:

Access to XMLHttpRequest at 'https://www.marvelcdb.com/api/public/decklist/103.json' from origin 'https://h54pzq.csb.app' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I am able to access the URL through other means, such as wget or simply navigating there in a browser. Only JavaScript applications seem to be blocked.

In the same codesandbox, I'm able to substitute a URL for a RIngsDB decklist and it works fine.

erlloyd commented 1 year ago

It looks like this is a problem if you use www.marvelcdb.com in the request url as opposed to just marvelcdb.com. In other words, https://marvelcdb.com/api/public/decklist/103.json works, while https://www.marvelcdb.com/api/public/decklist/103.json does not.

seastan commented 1 year ago

Thanks @erlloyd! Odd that RingsDB doesn't complain about www, but MarvelCDB does. Regardless, my issue has been resolved.

Kamalisk commented 1 year ago

I imagine I could make the redirect also have the cors headers, but I had no idea people still use www at all these days :P