简体   繁体   中英

Trying to get property of non-object error when using json

I keep getting the following error


: Trying to get property of non-object in on line :尝试在第行的获取非对象的属性

: Trying to get property of non-object in on line :尝试在第行的获取非对象的属性

I am using AngularJS to get the input from a form, sending the data to a php using console.log function and decoding the json file using php, then checking a database to find info matching the input values.

JS:

$scope.login = function(acc) {
    console.log(acc);//getting data input by user

    //post is used to create
    $http.post('php/login.php', {'user': $scope.acc.user, 'pass': $scope.acc.pass}).success(function(data) {
        console.log(data);
        if (data) {//row inserted
          // if not successful, bind errors to error variables
          console.log("Succesful");
          $scope.insertMessage = "Data inserted";
        } else {
          // if unsuccessful, bind success message to message
          $scope.insertMessage = "Data not inserted";
        }
        $scope.acc="";//reset values in form to empty
        //redirect to list
    });

    $http.get('php/login.php').success(function(logged){
            $scope.loggedin = logged;
            console.log($scope.loggedin);
      })
      .error(function(err){
          $log.error(err);
      });   
};

PHP:

<?php
$data = json_decode(file_get_contents("php://input"));

$user = $data->user;
$pass = $data->pass;

require_once("connection.php");

$conn = connectToDb();
$query= "SELECT count(*) 
FROM tbl_logdetails 
WHERE username = '$user' and password = '$pass'";
$result = mysqli_query($conn, $query) 
    or die("error in query: ".mysqli_error($conn));
$row = mysqli_fetch_row($result);
$count = $row[0];

echo $count;

if($count == 0){
    $logged = false;
}else{
    $logged=true;
}
echo json_encode($logged);

?>

Any help would be appreciated.

Aren't you calling login with POST and then login with GET right after each other? And you're not passing anything to the GET login attempt, hence the non-object when you use json_decode . So your script is working on the POST attempt but printing out the error messages on the GET attempt.

Remove this part:

$http.get('php/login.php').success(function(logged){
            $scope.loggedin = logged;
            console.log($scope.loggedin);
      })
      .error(function(err){
          $log.error(err);
      });   

The $http.get was redundant and was executing login.php again without giving any parameters, removing the $http.get was the solution...

$scope.login = function(acc) {
    console.log(acc);//getting data input by user

    //post is used to create
    $http.post('php/login.php', {'user': $scope.acc.user, 'pass': $scope.acc.pass}).success(function(data) {
        console.log(data);
        if (data) {//row inserted
          // if not successful, bind errors to error variables
          console.log("Succesful");
          $scope.insertMessage = "Data inserted";
          $scope.loggedin = data;
            console.log($scope.loggedin);
        } else {
          // if unsuccessful, bind success message to message
          $scope.insertMessage = "Data not inserted";
        }
        $scope.acc="";//reset values in form to empty
        //redirect to list

    });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM