AngularJS, PHP and json_decode


AngularJS, PHP and json_decode

我正试图将值从AngularJS发送到PHP,如下所示

loginService.js

'use strict';
app.factory('loginService',function($http)
{
   return{
       login:function(user){
                var promise = $http.post('data/user.php',user); //send data to user.php
                promise.then(function(msg){
                    if(msg.data=='succes') console.log('succes login');
                    else console.log('error login');   
                })
       }
   }
});

我的PHP文件如下

user.php

<?php
    $user=json_decode(file_get_contents('php://input'));
    if($user->mail=='foysal@gmail.com' && $user->pass=='1234')
    {   
        print 'succes';
    }
    else 
    {   
        print 'error';
    }
 ?>

我的输出低于

error login

loginCtrl.js

'use strict';
app.controller('loginCtrl',function($scope,loginService)
{
    $scope.login=function()
    {
    loginService.login();
    }
})

有人能说出我的错误在哪里吗??

感谢

我收到这样的消息"<br />↵<b>Notice</b>: Trying to get property of non-object in <b>D:'php'htdocs'login_angularjs'app'data'user.php</b> on line <b>14</b><br />↵""

我如何在这里调试??比如,php文件正在获取值吗??php文件正在发送值吗??

更新

我想我没能收集到价值观来自HTML表单。我在这里给出HTML表单代码。你能帮我吗找出问题??

login.tpl.html

Welcome : {{user.mail}}
<div class="bs-example">
    <form role="form" name="form1">
        <div class="form-group">
            <p>Welcome : {{user.mail}}</p>
            <label for="exampleInputEmail">Mail</label>
            <input type="text" placeholder="Enter name"
class="form-control" required ng-model="user.mail">
        </div>
        <div class="form-group">
            <label for="exampleInputPassword">Password</label>
            <input type="password" placeholder="Password"
id="exampleInputPassword" class="form-control" required
ng-model="user.pass">
        </div>
        <button class="btn btn-default" type="submit"
ng-disabled="form1.$invalid" ng-click="login(user)">Submit</button>
        <p></p>
    </form>
</div>

在PHP中需要这样的东西:

助手:

function getReturnObject ($message, $code) {
    $response = new StdClass();
    $response->message = $message;
    $response->code = $code;
    return $response;
}

主要代码:

print json_encode(getReturnObject('auth is successull', 200);

或者:

header('HTTP/1.0 403 Forbidden');
print json_encode(getReturnObject('auth is bad', 403);

然后在JavaScript中你需要这个:

var module = angular.module('myApp', []);
module.controller('myCtrl', ['$http', function ($http) {
  var user = null; // some user
  $http.post('data/user.php', user)
    .success(function (data) {
    // handle response there
    })
    .error(function (data) {
      console.log('error');
    });
}]);