Reputation: 121
I have a $http pst ajax call from angular js to Express.js api. In Express.js part I ma using async module of node to have some serial execution of code. When no issues I want to redirect to a specific page. Since I am making a ajax call from angular if I use res.render('url') this is not redirecting the page instead its alerting me on angularjs. BTW I have a $routeProvider on angular also :). That is to load partial template after redirect
Code of angular AJAX POST CALL
app.controller('MainCtrl',function($scope,$http){
$scope.loginForm=function(){
alert($scope.username);
$http.post('/login',{'name': $scope.username}).
success(function(data) {
alert(data);
}).
error(function(data) {
alert(data);
});
}
});
Node.js(Express.js) api to validate and make a redirect
app.post('/login',function(req,res){
var name= req.body.name;
//var password= req.body.password;
async.series([
function (callback){
var query= client.query('select count(*) as lgResult from Login where Email='+'"'+req.body.name+'"',function(err,result,field){
if(err) return callback(err);
if(result[0]['lgResult'] === 0){
res.send('passwordWrong');
}
if(result[0]['lgResult'] === 1){
callback(null, 'one');
}
});
},
function (callback){
var query= client.query('select count(*) as lgResult from Login where Email='+'"'+req.body.name+'"',function(err,result,field){
if(err) return callback(err);
if(result[0]['lgResult'] === 0){
res.send('passwordWrongSecondtime');
}
if(result[0]['lgResult'] === 1){
callback(null, 'two');
}
});
}],
function (err,results){
if(err) {
console.log(err);
res.send('Something Broke sorry');
}
else{
res.render('employelogin/employlogin');
}
});
});
Angular Routing :)
app.config(function($routeProvider, $controllerProvider,$compileProvider){
app.registerCtrl = $controllerProvider.register;
app.compileProvider = $compileProvider.directive;
$routeProvider
.when('/employeeTimeSheet',{
controller:'MainCtrl',
templateUrl:'employeeTimeSheet.html'
})
.when('/employeeLogin',{
templateUrl:'employeeDashboard.html'
})
.otherwise({redirectTo:'/employeeLogin'});
});
What is my mistake here
Upvotes: 0
Views: 2301
Reputation: 7119
Just do not try to handle everything within one HTTP request.
In your success callback use
$location.path('/employeeTimeSheet');
This will handle redirect. And don't forget to inject $location service to the controller.
Upvotes: 1