0

enter image description hereIn this code if/else is not working. Am I making any mistake? data.success contains true/false. If I code like this if (data.success === true) then else block is working and if block is not working and vise versa.

$scope.verifyMobile = function () {
   var otp = {
      "otp": $scope.mobile.otp
   };
   $http({
      method: 'POST',
      url: 'verify_mobile',
      data: otp,
      headers: {
         'Content-Type': 'application/x-www-form-urlencoded'
      }
   }).success(function (data, status, headers, config) {
      if (data.success) {
          $scope.verified = true;
          $scope.sms_sent = false;
      } else {
          alert(data.message);
      }
   }).error(function (data, status, headers, config) {
   });
};
8
  • try to print the data type of the value data.success, alert(typeof data.success) before the if condition, becuase undefined and null can also be checked with the same if condition, in case your data.success is undefined or null then else part will trigger Commented Jul 13, 2016 at 4:49
  • @Scary he already told angularjs probably he is a beginner in stackoverflow and coding that is why he tagged as java Commented Jul 13, 2016 at 4:52
  • if (data.success) will return true as long as data.success is not undefined nor null. Use if (data.success == true) instead. Commented Jul 13, 2016 at 4:53
  • isnt success deprecated ? try to use then Commented Jul 13, 2016 at 4:57
  • 2
    Change it to data.success[0], the success object is an array basing from your screenshot. Commented Jul 13, 2016 at 5:21

3 Answers 3

1

You should change the data.success and data.message to data.success[0] and data.message[0], because that are not boolean values you returning array in response that's why you have to take it in a array format. Try below code.

$scope.verifyMobile = function () {
        var otp = {
            "otp": $scope.mobile.otp
        };
        $http({
            method: 'POST',
            url: 'verify_mobile',
            data: otp,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).success(function (data, status, headers, config) {
            if (data.success[0]) {
                $scope.verified = true;
                $scope.sms_sent = false;
            } else {
                alert(data.message[0]);
            }
        }).error(function (data, status, headers, config) {
        });
};
Sign up to request clarification or add additional context in comments.

Comments

0

This is because your data.success is not contains boolean value. So before your if-else block try to print type of data.success

console.log(typeof data.success);

And see is it boolean if not then just resolve it.

1 Comment

when I do alert(typeof data.success), it returns object
0

Instead of .success() use .then().

Response will return an object you should check the response as below

$scope.httpRequest = function() {
  $http({
    method: 'GET',
    url: 'http://jsonplaceholder.typicode.com/posts/1',
  }).then(function(success) {
    if (success.data.userId === 1) {
      $scope.name = 'Jason Statham'
    } else {
      $scope.name = 'Cristiano Ronaldo'
    }
  }, function(error) {
    console.log(error)
  })
}

DEMO

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.