Kevin
Kevin

Reputation: 653

Access-Control-Allow-Origin' header is present on the requested resource

I am trying to login using google authentication using node.js, but it showing error like below.

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_ur…d=410427634474-u3tpasmj4r80s6v20o54s85fikhotl79.apps.googleusercontent.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Someone please help me to solve this, because i am trying this from past two days, still didn't fix.

Upvotes: 0

Views: 1663

Answers (2)

Rashid Javed
Rashid Javed

Reputation: 62

Write this in app.get function.

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

You can use node function as

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.');
  }
  });
});

Upvotes: 0

Jaldhi Oza
Jaldhi Oza

Reputation: 92

Try adding the following middleware to your NodeJS/Express app (I have added some comments for your convenience):

// 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();
});

Upvotes: 1

Related Questions