简体   繁体   中英

No 'Access-Control-Allow-Origin' in ajax request, but not by clicking on a <a> tag link

I am using HapiJS and Bell to authenticate users. I have an endPoint that is responsible to authenticate the users.

  server.route({
    method: ['POST','GET'],
    path: '/token',
    config: {
      auth: 'twitter',
      handler: function (request, reply) {

       reply(request.auth.credentials.profile)

      }
    }
  });

When I have an <a> tag link pointing to that endPoint everything works well, I am redirecting to twitter to authenticate and then I see the reply . What I want is to make that through ajax request so I can handle the response using then .

So I have something simple:

$.ajax({
  method: 'GET',
  url: 'http://localhost:8001/token'
}).then(function(data){
  console.log('>>>>>>>>>',data);
});

I click a button and I make the request, but in this case I have the following message on Chrome XMLHttpRequest cannot load https://api.twitter.com/oauth/authenticate?oauth_token=TPSmxB22abgI2jC8DvEgUxKOc4ZadsSI. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. XMLHttpRequest cannot load https://api.twitter.com/oauth/authenticate?oauth_token=TPSmxB22abgI2jC8DvEgUxKOc4ZadsSI. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. Any idea how can I solve that?

Because your session is from the HapiJS/Bell authentication process, making a GET to localhost is giving you CORS Header issues. You need to attach the headers serverside, wherever you spin your Node server up.

res.header("access-control-allow-origin","client domain including protocol")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM