简体   繁体   English

使用json时尝试获取非对象错误的属性

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

I keep getting the following error 我不断收到以下错误


Notice : Trying to get property of non-object in D:\\XAMPP\\htdocs\\website\\php\\login.php on line 4 注意 :尝试在第4行的D:\\ XAMPP \\ htdocs \\ website \\ php \\ login.php中获取非对象的属性

Notice : Trying to get property of non-object in D:\\XAMPP\\htdocs\\website\\php\\login.php on line 5 注意 :尝试在第5行的D:\\ XAMPP \\ htdocs \\ website \\ php \\ login.php中获取非对象的属性

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. 我正在使用AngularJS从表单获取输入,使用console.log函数将数据发送到php,并使用php解码json文件,然后检查数据库以查找与输入值匹配的信息。

JS: 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:

<?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? 难道不是彼此调用POST登录,然后立即使用GET登录吗? And you're not passing anything to the GET login attempt, hence the non-object when you use json_decode . 而且您没有将任何东西传递给GET登录尝试,因此,当您使用json_decode时,它是非对象。 So your script is working on the POST attempt but printing out the error messages on the GET attempt. 因此,您的脚本可以进行POST尝试,但是可以打印出GET尝试中的错误消息。

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... $ http.get是多余的,并且在不提供任何参数的情况下再次执行login.php,删除$ http.get是解决方案...

$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

    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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