错误:从源“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();
})
我的角度服务代码
当我单击使用 google 按钮登录时,此服务方法将被执行
addGoogleUser():Observable<any>{
const headers = new HttpHeaders()
headers.set('content-type','application/json')
return this.http.get("http://localhost:5000/auth/google",{headers:headers});
}