为什么GitHub API和CORS不在任何地方?

问题描述 投票:0回答:1

该应用是使用Create React App制作的,我正在尝试连接到GitHub API,但出现了CORS问题。因此,我决定使用CORS Anywhere Heroku应用程序。但这不起作用。

const url = `https://cors-anywhere.herokuapp.com/https://github.com/login/oauth/access_token?client_id${process.env.REACT_APP_CLIENT_ID}&client_secret=${process.env.REACT_APP_CLIENT_SECRET}&code=${code}`
const data = await fetch(url, {
    method: 'POST',
    headers: {
        "X-Requested-With": "XMLHttpRequest",
    },
})
(在异步等待功能内部)

我也尝试添加此标题:'Origin': 'http://localhost:3000'

然后取回给了我(所以当我登录data变量时:

enter image description here

我不知道怎么了。

javascript reactjs api github cors
1个回答
0
投票

我认为问题可能是您在发送到cors-anywhere heroku应用程序的URL中指定了协议https://。这是为了向您的浏览器指定您要使用哪个协议来请求资源,而heroku应用可能未考虑使用该协议。

尝试:

const url = https://cors-anywhere.herokuapp.com/github.com/login/oauth/access_token?client_id${process.env.REACT_APP_CLIENT_ID}&client_secret=${process.env.REACT_APP_CLIENT_SECRET}&code=${code}`
© www.soinside.com 2019 - 2024. All rights reserved.