0

I am working on my Front End (Angular Js calling a Rest Service) and this is working fine , i get a nice JSON as response:

$scope.test = function(response) {  
    $http.get("http://localhost:8080/androbridge/test/dotest"+"?s=ssss").success(function(response){
    $scope.related=JSON.stringify(response);
}); 

While this is not working :

var x;      
    $http.get("http://localhost:8080/androbridge/einloggen/loggeEin?username=" + $scope.username + "&password=" + $scope.password).success(function(response)
            {x=JSON.stringify(response);})  

    if(x=={"Kennzeichnung":"login","Status":true}){
        $rootScope.loggedIn=true;
        $location.path('/userPage');    
}else {
    alert('Wrong Login Information'+$scope.username +$scope.password + x)
}

Anyone see what mistake i make? I cant figure it out!

1
  • what error do you see in console?? Commented Nov 11, 2015 at 16:08

4 Answers 4

1

First off, you can't do object comparison like this:

if(x=={"Kennzeichnung":"login","Status":true}){

You could instead do something like this:

if(x.Kennzeichnung == 'login' && x.Status){

Also the $http.success() method is deprecated. You should use $http().then(successFn, errorFn); instead.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that the variable x is inside the $http scope, while you are testing it outside. The http call is asynchronous. Try this:

var x;
$http.get("http://localhost:8080/androbridge/einloggen/loggeEin?username=" + $scope.username + "&password=" + $scope.password).success(function(response) {
    x = JSON.stringify(response);
    if (x == {
            "Kennzeichnung": "login",
            "Status": true
        }) {
        $rootScope.loggedIn = true;
        $location.path('/userPage');
    } else {
        alert('Wrong Login Information' + $scope.username + $scope.password + x)
    }
});

1 Comment

Thanks Sir! Works like a charm .
0

First of all, $http is asynchron so your if will be executed before the response get received. Next thing is your if statement, it's syntactically incorrect.

Comments

0

You need to move all your code into the callback for the Promise. The promise callback is asynchronous and only get called once the response is coming back from the server. Your code block is currently outside the callback and will get executed immediately and not when the result is being returned.

Try something like this

    $http.get("http://localhost:8080/androbridge/einloggen/loggeEin?username=" + $scope.username + "&password=" + $scope.password)
.success(function(response)
            {

    var x = JSON.stringify(response);
    if(x == {"Kennzeichnung":"login","Status":true}){
        $rootScope.loggedIn=true;
        $location.path('/userPage');    
     }else {
      alert('Wrong Login Information'+$scope.username +$scope.password + x)
    };

});  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.