尽管寻找了几个小时,我还是找不到解决我的简单 cors 错误的方法。我在浏览器中收到以下信息:
访问 XMLHttpRequest 'https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3001%2Fapi%2Fauth%2Fauth%2Fgoogle%2Fcallback&scope=profile%20email&client_id =*****.apps.googleusercontent.com'(从'http://localhost:3000/api/auth/auth/google'重定向)来自来源'http://localhost:3000'已被阻止CORS 策略:请求的资源上不存在“Access-Control-Allow-Origin”标头。
出于安全原因,我只是在这篇文章中替换了客户端 ID。 当我在 Postman/Thunder 客户端中进行此调用时,它会返回 200 响应,并且似乎工作正常
所以我尝试在express中使用cors NPM包app.use(cors());也尝试在server.js中使用这个函数app.all("/*", function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); next(); });我也尝试使用`
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(proxy("/api/auth/auth/google", {
target: "https://localhost:3000/",
changeOrigin: true
}));
}
Run Code Online (Sandbox Code Playgroud)
我刚刚完成了书中找到的所有解决方案,但到目前为止似乎没有一个可行。任何帮助将不胜感激这是我的代码:
客户端中的Package.json
{
"name": "tasktracker",
"proxy": "http://localhost:3001/",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.13.0",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"axios": "^0.21.1",
"http-proxy-middleware": "^2.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"web-vitals": …Run Code Online (Sandbox Code Playgroud)