产地空不被访问控制允许来源允许

产地空不被访问控制允许来源允许

本文介绍了护照AngularJS防爆pressJS:产地空不被访问控制允许来源允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图用angular.js的$ HTTP模块授权Twitter的应用程序,我总是得到:

When I'm trying to use $http module of angular.js for authorizing twitter app I always get:

XMLHttpRequest cannot load https://api.twitter.com/oauth/authenticate?oauth_token=something. Origin null is not allowed by Access-Control-Allow-Origin.

客户端code:

Client Code:

$http({
   method: 'GET',
   headers: { "Content-Type": undefined },
   url: '/oauth/twitter'
});

服务器code:

Server Code:

app.configure(function () {
   app.use(express.cookieParser());
   app.use(express.cookieSession({ secret: 'tobo!', cookie: { maxAge: 3600 }}));

   app.use(express.session({secret: 'secret'}));
   app.use(passport.initialize());
   app.use(passport.session());
});

app.all('*', function(req, res, next) {
   res.header("Access-Control-Allow-Origin", "*");
   res.header("Access-Control-Allow-Credentials", true);
   res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
   res.header("Access-Control-Allow-Headers",
    'Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept');
   next();
});

app.get('/oauth/twitter', passport.authenticate('twitter'), function (req, res) {
// The request will be redirected to Twitter for authentication, so this
// function will not be called.
console.log('ouath/twitter')
});

app.get('/oauth/twitter/callback', passport.authenticate('twitter', { successRedirect:  '/', failureRedirect: '/login' }));

但是,如果使用通过OAuth /微博地址的超链接它工作正常。我不知道是什么问题。大多数表示,原产地是不允许的,但'*'应该让每一个地址连接到服务器。

But it works ok if I use a hyperlink with oauth/twitter address. I don't know what is the problem. Most said that origin is not allowed, but '*' should allow every address to be connected to server.

推荐答案

我有,我发现了同样的问题,唯一的解决办法是使用超链接(如您在您的文章说的)。我认为你不能使用安全原因Ajax请求。不过使用为我工作的超链接,我可以授权我的用户。

I had the same problem and the only solution that I have found is to use an hyperlink (as you say in your post). I think that you can't use a ajax request for security reason. However using an hyperlink worked for me and I can authorize my users.

你为什么不使用超链接?有一些问题?

Why don't you use an hyperlink? Is there some problem?

这篇关于护照AngularJS防爆pressJS:产地空不被访问控制允许来源允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 20:52