我正在尝试使用node.js使用google身份验证登录,但它显示如下所示的错误。
XMLHttpRequest无法加载https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_ur ... d = 410427634474-u3tpasmj4r80s6v20o54s85fikhotl79.apps.googleusercontent.com。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点'null'访问。
有人请帮我解决这个问题,因为我从过去两天开始尝试这个,仍然没有解决。
答案 0 :(得分:3)
尝试将以下中间件添加到您的NodeJS / Express应用程序中(为方便起见,我添加了一些注释):
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
答案 1 :(得分:0)
在app.get函数中写这个。
res.set('Access-Control-Allow-Origin', '*');
您可以将节点功能用作
app.get("/png",function(req,res){
res.set('Access-Control-Allow-Origin', '*');
var sr = { data: "", message: "", error: "" };
console.log(222);
connection.query('SELECT * FROM png ', function(err, rows, fields) {
connection.end();
if (!err)
{
var userr = rows;
sr.data = userr;
res.json(sr);
} else{
console.log('Error while performing Query.');
}
});
});