1

Here is client side in AngularJS (works fine):

$scope.ajaxLogin = function(){
    var fn = document.getElementById("username").value;
    var pw = document.getElementById("password").value;
    $http({
        url: "myurl", 
        method: "POST",
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        data: { username: fn, password: pw }
    }).success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
        $scope.showAlertError();
    }).error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        $scope.showAlertError();
    });
};

I expect to have success only if user and password are matched. Server side:

if ($result=mysqli_query($con,$sql))
{     
    $rowcount=mysqli_num_rows($result);
    printf($rowcount);
    mysqli_free_result($result);
}

Before AngularJS I used normal AJAX handling. this is how i did earlier:

  function ajax_post(){
      // Create our XMLHttpRequest object
      var hr = new XMLHttpRequest();
      // Create some variables we need to send to our PHP file
      var url = "myurl";
      var fn = document.getElementById("username").value;
      var ln = document.getElementById("password").value;
      var vars = "username="+fn+"&password="+ln;
      hr.open("POST", url, true);
      // Set content type header information for sending url encoded variables in the request
      hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      // Access the onreadystatechange event for the XMLHttpRequest object
      hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
          var return_data = hr.responseText;

        if(return_data=="1"){

          console.log("this is return data"+return_data);

        }else{

          ons.notification.alert({message: 'Login Failed!'});

        }

        }
      }
      // Send the data to PHP now... and wait for response to update the status div
      hr.send(vars); // Actually execute the request

  }

How can I use this with AngularJS? I tried putting similar code in success handler, but it always goes to else block.

2 Answers 2

1

did you try this:

.success(function(data, status, headers, config) {
  if(status === 200) {
    var return_data = data;
    if(return_data==="1"){
      location.href = "home.html?username=" + fn;
    }
    else{
      ons.notification.alert({message: 'Login Failed!'});
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

do you mean you are getting: {message: 'Login Failed!'}, which would mean that return_data !=="1"? What is the value of data/return_data?
please add in the success function: console.log(data, status) and post here
it gives 0, 200 . but the problem is when i used my old method it gives 1. there is no way it gives 0. you can see my php code above. am i sending my data correctly? i think problem is that
it worked. after i Decode the JSON in PHP file. problem was AngularJS sends data like JSON i didnt know about that before. thanks for the help
1
$scope.ajaxLogin = function(){
    var fn = document.getElementById("username").value;
    var pw = document.getElementById("password").value;
     $http({
    url: "myurl", 
    method: "POST",
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    data: { username: fn, password: pw }
    }).success(function(data, status, headers, config) {


                                       //Your server output is received here.
      if(data){console.log('username and password matches');}
      else {console.log('username and password does not match');}



 $scope.showAlertError();
    }).
  error(function(data, status, headers, config) {
   // called asynchronously if an error occurs
   // or server returns response with an error status.
   $scope.showAlertError();

    });

  };

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.