egoist / gh-pinned-repos

API service for retrieving pinned repos on GitHub.
https://gh-pinned-repos.egoist.dev
MIT License
116 stars 17 forks source link

Problems with CORS policy #13

Closed joseluisgs closed 2 years ago

joseluisgs commented 2 years ago

Hi. I have a problem form Cors policy. My website worked ok, but now fails when try to get my repo info, Could you help me?

https://joseluisgs.github.io/proyectos/#repositorio (My website) I use a Vue component in my VuePress page This is my component https://github.com/joseluisgs/joseluisgs/blob/master/web/docs/.vuepress/components/ReposPinned.vue

I get the repo info with async mounted() { // https://gh-pinned-repos.now.sh/ const response = await fetch("https://gh-pinned-repos.now.sh/?username=joseluisgs"); const data = await response.json(); this.repositorios = data; }

But, now, I have this problem Access to fetch at 'https://gh-pinned-repos.vercel.app/?username=joseluisgs' (redirected from 'https://gh-pinned-repos.now.sh/?username=joseluisgs') from origin 'https://joseluisgs.github.io' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I try to add no-cors value, or change to use axios, but I always have the same problem const url = 'https://gh-pinned-repos.now.sh/?username=joseluisgs'; const config = { method: 'GET', mode: 'no-cors', }; try { const response = await axios.get(url, config); console.log(response.data); } catch (error) { console.error(error); }

I checked your project and you have / allow cors from any origin / res.setHeader('Access-Control-Allow-Origin', '') res.setHeader('Access-Control-Request-Method', '') res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET') res.setHeader('Access-Control-Allow-Headers', '*')

Please, coudl yopu help me to solve it

Thank you very much Jose

joseluisgs commented 2 years ago

I've tryed with differente optios, also I changed de URL const instance = axios.create({ baseURL: "https://gh-pinned-repos.vercel.app/?username=joseluisgs", withCredentials: false, headers: { 'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Methods':'GET', } });

response = await instance.get(); console.log(response.data);

I always have the same problem

Access to XMLHttpRequest at 'https://gh-pinned-repos.vercel.app/?username=joseluisgs' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request. xhr.js:210 GET https://gh-pinned-repos.vercel.app/?username=joseluisgs net::ERR_FAILED

Thank you

egoist commented 2 years ago

Not sure about this, I did a simple fetch and it works fine https://jsbin.com/jomicon/edit?js,console

egoist commented 2 years ago

It seems using gh-pinned-repos.egoist.sh would work.

joseluisgs commented 2 years ago

Thank you very much!!! No problems with the new URL 👍