ajax请求中没有“Access-Control-Allow-Origin”,但没有点击<a> tag link

时间:2015-05-30 05:29:38

标签: javascript ajax node.js cors hapijs

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. Any idea how can I solve that?

2 个答案:

答案 0 :(得分:1)

因为您的会话来自HapiJS / Bell身份验证过程,所以对localhost进行GET会给您带来CORS Header问题。无论您在哪里旋转节点服务器,都需要附加服务器端头。

答案 1 :(得分:-1)

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