0

On click of a button I send a post request to express through angularjs. In express' post, I redirect to another url. But nothing happens.

When I give a non-existing url, in the console I see a 404 error. When I give a correct existing url, it does absolutely nothing. Doesn't show any error either.

The express code:

   app.get('/login',function(request,response){ //the file sent when /login is requested
    response.sendFile(__dirname+"/staticFolder/view/login.html");
})

    app.post('/loginCheck', function(req, res, next) { //called by angular controller when button clicked

      res.redirect('http://localhost:4000/'); //supposed to show index.html
    });
    app.get('/',function(request,response){
        response.sendFile(__dirname+"/staticFolder/view/");
    })

The controller:

    .controller('loginController',function($scope,$http){
    $scope.login=""
    $scope.password="";

    $scope.mysqlError="";

    $scope.loginFunc = function(){


        $http({
            method:'POST',
            url:'/loginCheck',
            data:{
               login: $scope.login,
               password: $scope.password
            }
        })
        .then(function successCallback(response) {

        if(response.data=='invalid'){
            $scope.mysqlError="mysqlError";
        }

        });

    }
})
  • response.sendFile(__dirname+"/staticFolder/view/"); add some view file which you want to render after /view/ – owais Aug 17 '16 at 09:17
  • @owaishanif786.i tried it. it still does nothing. –  Aug 17 '16 at 09:20
  • what did you see when you acess `http://localhost:4000/` on your pc ? – Thanh Nguyen Van Aug 17 '16 at 09:27
  • @ThanhNguyenVan. i can see the `index.html` when I go to `http://localhost:40‌​00/` –  Aug 17 '16 at 09:28
  • your app has already listened in port 4000 so just do `res.redirect('http://localhost');`, then test a again. – Thanh Nguyen Van Aug 17 '16 at 09:31
  • try to redirect some other page like res.redirect("http://www.google.com"); – owais Aug 17 '16 at 09:37
  • @owaishanif786. in the console it says `GET http://localhost:4000/google.com 404 (Not Found)` when i do `res.redirect("google.com");` –  Aug 17 '16 at 09:42
  • @ThanhNguyenVan. it didn't work.. –  Aug 17 '16 at 09:43
  • try full url redirect res.redirect("http:/‌​/www.google.com"); – owais Aug 17 '16 at 09:46
  • @owaishanif786. that didn't work either –  Aug 17 '16 at 09:49
  • ah, localhost is a default url for node, so try `res.redirect("/");` it will redirect to home page. – Thanh Nguyen Van Aug 17 '16 at 09:49
  • @ThanhNguyenVan. that's the first thing I tried..i then tried with `localhost`. nothing works. is it possible to redirect from a `post`? –  Aug 17 '16 at 09:53
  • @ThanhNguyenVan. ok i tried it now. I get `angular.js:12011 POST http://localhost:4000/ 404 (Not Found)`. But when I try to go to `http://localhost:4000/ ` by typing in address bar, I get the index.html. When I do it like in the link you gave, I get 404 error for the same url –  Aug 17 '16 at 10:04
  • Perhaps follow this http://stackoverflow.com/questions/11570301/res-redirect-from-post – Thanh Nguyen Van Aug 17 '16 at 10:06
  • @ThanhNguyenVan. i have to send a response and show other page using angularjs based on the response. thanks a lot for trying to help :) –  Aug 17 '16 at 10:20

0 Answers0