【发布时间】:2021-11-18 12:06:48
【问题描述】:
错误:从源“http://localhost:4200”重定向到“http://localhost:5000/auth/google”)已被 CORS 策略阻止:没有“Access-Control-Allow-Origin”标头存在于请求的资源上。
即使我在服务器中使用 cors 模块,我也会收到此错误。
const cors = require('cors')
app.use(cors())
也试过这个而不是 cors
app.use((req, res, next) => {
console.log("entered cors")
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET , PUT , POST , DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type, x-requested-with");
next();
})
我的角度服务代码
当我点击使用谷歌按钮登录时,此服务方法将被执行
addGoogleUser():Observable<any>{
const headers = new HttpHeaders()
headers.set('content-type','application/json')
return this.http.get("http://localhost:5000/auth/google",{headers:headers});
}
【问题讨论】:
-
您不必在有角度的一侧设置这些标题.. 这可能实际上会导致 CORS 问题..
-
你修好了吗?
-
删除 Angular 中的标题也没有解决
标签: node.js angular express cors passport-google-oauth2