2
votes

This is really weird. I am using AngularJS in my app. During login, I make an HTTP POST request ; data is sent properly and I receive the right response. Then I logout - which returns me back to the login page - and I do the same http req but the data is not sent by the post request. Upon console.log I see that the $scope data is correct - just the POST data is not being sent. If I do a hard refresh of the login page it works again. So my problem is that consecutive requests are not being made without refreshes. Here is my login function -

$scope.login = function() {    
        var request = $http({
            method: "POST",
            url: URL + "login",
            crossDomain: true,
            data: this.loginData
        });
        request.success(function(data) {                  
            var response = angular.fromJson(data);   
            if(!response["error"]) {
                sessionStorage.email = response["email"];
                sessionStorage.password = response["password"];
                sessionStorage.userId = response["id"];                
                $location.path('/dashboard');                        
            } else {
                $scope.responseMessage = response["message"][0];
            }

        });
        request.error(function(data) {
            console.log(data);
        });
    }

And this is my logout function -

$scope.logout = function() {
        sessionStorage.clear();        
        $location.path("/login");
    }
1
Well this is probably a terrible answer but at the top just put var request = null; It will be to do with the promise not being resolved properly somehow. - rtn
That did not work :( - Prarthana Hegde
Try accessing this.loginData via $scope. i.e. $scope.loginData - rtn
Didn't work either! - Prarthana Hegde
What router are you using? try using that to navigate back to login page so it will trigger the proper angular life cycle destroy events and also try calling $route.reload(); or $state.reload(); in the logout method. - rtn

1 Answers

0
votes

Found the answer and posting it here in case anyone runs into a similar problem. Setting a header for content type works:

var request = $http({
            method: "POST",
            url: URL + "login",
            crossDomain: true,
            headers: {
                "content-type": "application/json"
            },
            data: this.loginData
        });