過去6個小時我一直在嘗試在互聯網上發現的大量事情,而我似乎無法解決這個問題。NodeJS OAuth2與AngularJS的CORS問題
我有一個本地測試角的應用程序,在http://tok.client
我有FrankHassanabad的OAuth2orize食譜(https://github.com/FrankHassanabad/Oauth2orizeRecipes)下
http://localhost:3000
對於我的生活,我不能讓向OAuth2服務器發送令牌請求。
客戶端代碼:
.config(function($stateProvider, $urlRouterProvider, $httpProvider) { //Reset headers to avoid OPTIONS request (aka preflight) $httpProvider.defaults.headers.common = {}; $httpProvider.defaults.headers.post = {}; $httpProvider.defaults.headers.put = {}; $httpProvider.defaults.headers.patch = {}; $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; console.log($httpProvider.defaults.useXDomain); console.log($httpProvider.defaults.headers.common); })
$scope.doLogin = function() {
console.log('DUMMY - doLogin');
return $http.post('https://localhost:3000/oauth/token', 'grant_type=password&username=bob&password=secret&scope=offline_access',{
headers: {
'Authorization': 'Basic YWJjMTIzOnNzaC1zZWNyZXQ',
'Access-Control-Allow-Origin': 'https://localhost:3000',
'Accept': 'application/x-www-form-urlencoded',
'Content-Type': 'application/x-www-form-urlencoded'
}
}
)
.success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
})
.error(function(dataX, status, headersX, config) {
console.log(dataX);
console.log(status);
console.log(headersX);
console.log(config);
// called asynchronously if an error occurs
// or server returns response with an error status.
});
//fetchService();
};
服務器端代碼
//Session Configuration
app.use(function(req, res, next){
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Origin, Accept, Authorization, Content-Type');
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}
},express.session({
secret: config.session.secret,
store: sessionStorage,
key: "authorization.sid",
cookie: {maxAge: config.session.maxAge }
}));
我嘗試添加所有CORS正常運作的要求,我能找到,它不工作。
這是我在瀏覽器中得到的響應:https://www.dropbox.com/s/zcujugh2m4i0xml/Screenshot%202014-03-26%2000.46.51.png
我甚至不知道我在臨時標題設置的標題,我讀了一噸的文章和StackOverflow上的問題,我只是想不通出來。
在某些時候,我雖然是某種https問題,但我在本地有https,OAuth2演示完美地工作,Chrome中的REST控制檯也工作得非常好,只有AngularJS決定讓我花很多錢小時試圖解決這個:)。
可能是什麼問題?
顯然,通過使用信息從這個問題http://stackoverflow.com/questions/3102819/disable-same-origin- policy-in-chrome/6083677#6083677 ...禁用Chrome的安全性我可以成功發佈帖子。這不是一個解決方法,但爲了目前的測試目的,它現在可以工作了,現在我只需要看看它是如何在科爾多瓦的網絡工作者中工作的。 –