【问题标题】:Trying to get property of non-object error when using json,php使用json,php时尝试获取非对象错误的属性
【发布时间】:2016-11-09 07:31:07
【问题描述】:

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

登录.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";
        }
?>

【问题讨论】:

  • 你不打算使用 $_POST/$_REQUEST 从请求中获取数据吗?而且,您使用 file_get_contents 的方式错误
  • 你能给我看个样本@Semi-Friends
  • @RaviKumar : 你能试试我的代码吗?

标签: javascript php mysql angularjs json


【解决方案1】:

基本上问题出在标题内容类型上。 要么像这样编码,要么让我知道你是否仍然没有在 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.
?>

【讨论】:

  • 我想在 php 文件 @Jigar7521 中使用它
  • 你可以在你的 php 文件中包含所有这样的数据: $data = json_decode(file_get_contents("php://input")); 将其替换为 $data = $_POST;
  • 如果您想让我制作工作代码 sn-p,请告诉我
  • 用户名;包括(“包括/db.php”); //var_dump($_POST['username']); $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"; } 其他 { 回声“0”; } ?> 这是正确的@Jigar7521
  • 我不明白你在说什么
猜你喜欢
  • 2016-04-15
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2014-05-08
相关资源
最近更新 更多