-1

错误:从源“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});
}
4

2 回答 2

-1

在您的快递服务中:

res.header("Access-Control-Allow-Origin", '*');

或者

res.header("Access-Control-Allow-Origin", "http://localhost:4200");

并确保app.use((req, res, next) => {}在拨打其他路线之前先拨打电话。

于 2021-09-26T03:22:45.233 回答
-1

试试下面 -

app.get('/auth/google', cors(), (req, res) => {
        // your get code
 });
于 2021-09-25T16:56:53.757 回答