Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
do you get any errors? what are they? –  Eliran Malka Mar 3 at 11:29
    
I am not getting error, instead of redirecting I am getting a alert on my angular js side in success. –  Madhu Mar 3 at 11:33
    
@EliranMalka Atlast found a way for this to work, not sure if its the best way res.contentType('application/html'); var data = 'emplogin/empr/:'+results; res.header('Content-Length', data.length); res.end(data); And on the Angularjs Side we can use $windows.location=data; –  Madhu Mar 3 at 13:53
add comment

1 Answer

Just do not try to handle everything within one HTTP request.

  1. In Express app use res.send(200, 'ok'); (or something similar) instead of res.render(); This makes Angular run .success() callback.
  2. res.send('Something broke') will trigger Angular's success callback as it sends 200 HTTP code by default, so add error status code to the response. e.g. res.send(500, 'Something broke'); In that case Angular will run .error callback.
  3. In Angular success callback redirect to desired page and let Angular decide which view to request from the server. (hint: $location service is very helpful here http://docs.angularjs.org/guide/dev_guide.services.$location)

In your success callback use

$location.path('/employeeTimeSheet');

This will handle redirect. And don't forget to inject $location service to the controller.

share|improve this answer
    
Your Idea works! But there is something else that I want...I want the whole page to be redirected to a different page, and express has some results coming in I will have to print them on the page. Say a new employee dashboard page is where I have to redirect to. There is a new template design which I have to show :) –  Madhu Mar 3 at 11:45
    
May be res.redirect('/path/to/new/page'); will do the job? This will redirect your entire app to a new location: app.com will be app.com/path/to/new/page after that. –  Eugene Kostrikov Mar 3 at 11:59
    
With res.redirect this is what I get on console 302 Moved Temporarily 31ms –  Madhu Mar 3 at 12:18
    
Yep, this is what express sends with response. Now your browser should follow redirect and change path. –  Eugene Kostrikov Mar 3 at 12:23
    
I am getting response on angularjs...a alert on success is coming on angularjs side and redirect is not happening :) –  Madhu Mar 3 at 12:41
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.