【发布时间】:2015-06-06 04:39:44
【问题描述】:
我为我的网络应用构建了一个 API,它是使用 MEAN 堆栈构建的。 现在我正在尝试在使用 Ionic Framework 构建的移动客户端上使用这个 API。 我正在使用此代码对 API 执行 $http 调用:
$http.post(ServerIP+'/login', {username: $scope.credentials.username, password: $scope.credentials.password}).success(function(response) {
$scope.authentication.user = response;
$location.path('/');
}).error(function(response) {
$scope.error = response.message;
});
它通过用户对象获得有效响应,但如果我尝试从 API 的受保护部分获取一些信息,它不起作用并且正在重置身份验证。
在网络应用程序上,我使用相同的代码并且一切正常。 此问题仅发生在 Ionic 应用程序上。 我已经这样设置了 CORS:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' === req.method) {
res.sendStatus(200);
}
else {
next();
}
});
请帮帮我!
【问题讨论】:
-
你是在生成并使用tokens或一些策略来匹配req吗?
-
您是否设置 withCredentials = true globaly?
-
我正在使用 passport-local-mongoose 作为本地策略。
标签: angularjs node.js ionic-framework ionic mean-stack