簡體   English   中英

處理$ http AngularJS返回數據

[英]Handling $http AngularJS return data

這是AngularJS的客戶端(可以正常工作):

$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();
    });
};

我希望只有在用戶名和密碼匹配時才能成功。 服務器端:

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

在AngularJS之前,我使用普通的AJAX處理。 這是我之前所做的:

  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

  }

如何在AngularJS中使用它? 我嘗試將類似的代碼放入成功處理程序中,但它總是轉到else塊中。

您是否嘗試過:

.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!'});
    }
  }
}
$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();

    });

  };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM