当谷歌使用http.get方法从前端登录时,angular中出现CORS策略错误



https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_uri=http%3A%2F%2localhost%3A5000%2Auth%2Google%2Fcallback&scope=https%3A%2F%2Fww.googleapis.com%2Fauth%2Fplus.login%20https%3A+2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&client_id=46544304857-3faafatnn26pmf2hgsjmsidels9p962.apps.googleusercontent.com:

请求的资源上不存在"Access Control Allow Origin"标头。因此,不允许访问源"null"。

安装cors模块添加以下代码:

const whitelist = ['http://localhost:5000'];
const corsOptions = {
origin (origin, callback) {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
}
};
app.options('*', cors()); // CORS pre-flight
app.use(cors(corsOptions));

请参见whitelist阵列。在此处添加要列入白名单的url。

例如:将google.com添加到阵列中。

最新更新