简体   繁体   English

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

[英]Trying to get property of non-object error when using json,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.here my code 我使用AngularJS从表单获取输入,使用console.log函数将数据发送到php并使用php解码json文件,然后检查数据库以查找与输入值匹配的信息。我的代码

login.html 的login.html

<div ng-controller="loginCtrl">
<form action="/" id="mylogin">
    Username: <input type="text" id="username" ng-model="username" ><br>
    Password: <input type="password" id="password1" ng-model="password1">
    <button type="button" ng-click="submit()">Login</button>
</form>

controller.js controller.js

 app.controller('loginCtrl', function ($scope, $http) {
 $scope.submit = function () {
    alert($scope.username);
        $http.post('php/userlogin.php',{'username' : $scope.username}).success(function(data){
            console.log(data);
            if (data == true) {
                alert('aaa');
                                }
            });
            } 
});

php/userlogin.php PHP / userlogin.php

<?php 
  $data = json_decode(file_get_contents("php://input"));
  $user=$data->username;
  include("include/db.php");
  $select_user = mysql_query("select * from userlogin where username='".$user."'" );
  $result = mysql_fetch_array($select_user);
  $user_id=$view_prof['user_id'];
    if($user_id != "" )
        {
            echo "1";
        }
    else
        {
            echo "0";
        }
?>

Basically problem is in header content type. 基本上问题在于标题内容类型。 Either Code it like this or let me know if you still not get post variable in php : 要么是这样的代码还是让我知道你是否仍然没有在php中获得post变量:

controller 调节器

 app.controller('loginCtrl', function ($scope, $http) {
 $scope.submit = function () {
    alert($scope.username);
        $http.post('php/userlogin.php',{'username' : $scope.username},{transformRequest: angular.identity, headers: {'Content-Type': undefined}).success(function(data){
            console.log(data);
            if (data == true) {
                alert('aaa');
                                }
            });
            } 
});

php file php文件

<?php 
var_dump($_POST); // you will surely get this.
?>

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

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