angularjs 如何从服务器获取响应数据
How to get response data from the server in angularjs
这是我的场景。 nodejs 中的服务器处理身份验证过程,而在前端我们有 angularjs。当用户点击按钮时,他使用 Facebook 登录,然后服务器处理身份验证的所有方面,最后重定向到 angularjs 应用程序的 uri。我们在服务器中有类似的东西
module.exports = function(request, reply) {
if (request.auth.isAuthenticated) {
var profile = request.auth.credentials.profile.raw;
// set to cookie
request.auth.session.set(profile);
// Perform any account lookup or registration, setup local session,
// and redirect to the application. The third-party credentials are
// stored in request.auth.credentials. Any query parameters from
// the initial request are passed back via request.auth.credentials.query
// here we should redirect the app flow somewhere
return reply({ profile: profile.id }).redirect('http://localhost:8080/app');
}
return reply('Unauthorized').code(401);
};
我的问题是我不知道如何在 angularjs 中检索配置文件对象。我的意思是我知道存在 $http
提供商,但在以下情况下,请求不是从 angularjs 开始的。如果用户签名成功,服务器会回复 SPA
$http.get('/app')
.success(function(data){
console.log(data);
});
您可以使用 $routeProvider
作为 URL 参数发送
您的配置应如下所示:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/login/:profileId', {
templateUrl: 'template.html',
controller: 'loginCtrl'
}).
otherwise({
redirectTo: '/home'
});
}]);
您的控制器:
app.controller("loginCtrl",function($routeParams,$scope){
$scope.profileId = $routeParams.profileId;
//YOU CAN REDIRECT HERE TO ANOTHER VIEW AFTER
})
后端
module.exports = function(request, reply) {
if (request.auth.isAuthenticated) {
var profile = request.auth.credentials.profile.raw;
// set to cookie
request.auth.session.set(profile);
// Perform any account lookup or registration, setup local session,
// and redirect to the application. The third-party credentials are
// stored in request.auth.credentials. Any query parameters from
// the initial request are passed back via request.auth.credentials.query
// here we should redirect the app flow somewhere
return reply({ profile: profile.id
// use # for html5 mode
}).redirect('http://localhost:8080/app/#/login/'+profile.id);
}
return reply('Unauthorized').code(401);
};
这是我的场景。 nodejs 中的服务器处理身份验证过程,而在前端我们有 angularjs。当用户点击按钮时,他使用 Facebook 登录,然后服务器处理身份验证的所有方面,最后重定向到 angularjs 应用程序的 uri。我们在服务器中有类似的东西
module.exports = function(request, reply) {
if (request.auth.isAuthenticated) {
var profile = request.auth.credentials.profile.raw;
// set to cookie
request.auth.session.set(profile);
// Perform any account lookup or registration, setup local session,
// and redirect to the application. The third-party credentials are
// stored in request.auth.credentials. Any query parameters from
// the initial request are passed back via request.auth.credentials.query
// here we should redirect the app flow somewhere
return reply({ profile: profile.id }).redirect('http://localhost:8080/app');
}
return reply('Unauthorized').code(401);
};
我的问题是我不知道如何在 angularjs 中检索配置文件对象。我的意思是我知道存在 $http
提供商,但在以下情况下,请求不是从 angularjs 开始的。如果用户签名成功,服务器会回复 SPA
$http.get('/app')
.success(function(data){
console.log(data);
});
您可以使用 $routeProvider
作为 URL 参数发送您的配置应如下所示:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/login/:profileId', {
templateUrl: 'template.html',
controller: 'loginCtrl'
}).
otherwise({
redirectTo: '/home'
});
}]);
您的控制器:
app.controller("loginCtrl",function($routeParams,$scope){
$scope.profileId = $routeParams.profileId;
//YOU CAN REDIRECT HERE TO ANOTHER VIEW AFTER
})
后端
module.exports = function(request, reply) {
if (request.auth.isAuthenticated) {
var profile = request.auth.credentials.profile.raw;
// set to cookie
request.auth.session.set(profile);
// Perform any account lookup or registration, setup local session,
// and redirect to the application. The third-party credentials are
// stored in request.auth.credentials. Any query parameters from
// the initial request are passed back via request.auth.credentials.query
// here we should redirect the app flow somewhere
return reply({ profile: profile.id
// use # for html5 mode
}).redirect('http://localhost:8080/app/#/login/'+profile.id);
}
return reply('Unauthorized').code(401);
};