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

时间:2016-11-09 07:31:07

标签: javascript php mysql angularjs json

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

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

 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 
  $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";
        }
?>

1 个答案:

答案 0 :(得分:0)

基本上问题在于标题内容类型。 要么是这样的代码还是让我知道你是否仍然没有在php中获得post变量:

控制器

 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文件

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