我正在使用HapiJS
和贝尔对用户进行身份验证。我有一个端点负责对用户进行身份验证。
server.route({
method: ['POST','GET'],
path: '/token',
config: {
auth: 'twitter',
handler: function (request, reply) {
reply(request.auth.credentials.profile)
}
}
});
当我有 <a>
时指向该端点的标签链接一切正常,我重定向到 Twitter 进行身份验证,然后我看到 reply
。我想要的是通过 ajax
来实现这一点请求,以便我可以使用 then
处理响应.
所以我有一些简单的事情:
$.ajax({
method: 'GET',
url: 'http://localhost:8001/token'
}).then(function(data){
console.log('>>>>>>>>>',data);
});
我单击一个按钮并发出请求,但在这种情况下,我在 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.
知道如何解决这个问题吗?
最佳答案
因为您的 session 来自 HapiJS/Bell 身份验证过程,所以对 localhost 进行 GET 会给您带来 CORS header 问题。无论您在何处启动 Node 服务器,都需要在服务器端附加 header 。
关于javascript - ajax 请求中没有 'Access-Control-Allow-Origin',但不是通过单击 <a> 标记链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30542621/