简体   繁体   English

AngularJS,PHP和json_decode

[英]AngularJS, PHP and json_decode

I am trying to send values from AngularJS to PHP like below 我正在尝试将值从AngularJS发送到PHP,如下所示

loginService.js 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');   
                })
       }
   }
});

My PHP file is like below 我的PHP文件如下

user.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';
    }
 ?>

My output is like below 我的输出如下

error login

loginCtrl.js loginCtrl.js

'use strict';

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

Could anyone say where is my mistake?? 谁能说我的错误在哪里?

Thanks 谢谢

I am getting a message like this " <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 />↵" " 我收到这样的消息“ <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 />↵" ”行

How can I debug here ?? 我如何在这里调试? Like, is php file getting values ?? 就像,是php文件获取值? is php file sending values ?? 是PHP文件发送值?

UPDATE 更新

I think I failed to gather the values from the HTML Form. 我想我无法从HTML表单中收集值。 I am giving HTML Form code here.Could you help me to figure out the issue ?? 我在这里提供HTML表单代码。您能帮我解决这个问题吗?

login.tpl.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>

You need something like this in PHP: 您在PHP中需要这样的东西:

Helper: 帮手:

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

Main code: 主要代码:

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

Or: 要么:

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

Then in JavaScript you need this: 然后在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');
    });
}]);

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

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