AngularJS表单返回undefined

时间:2015-12-16 16:18:55

标签: javascript php angularjs

我正在编写一个简单的登录服务,我只是想创建用户名和密码,将它们发送到php并获得简单的console.log' GJ'。一切正常,直到我得到回应'错误'从我的PHP函数。我试着在我发送的console.log数据中说它未定义。我试着用JSON字符串化同样的东西。任何人都可以检查我错在哪里

HTML

       <div class="container login">
       <div class="column column-4">
        <form  name="from1" role="form">

        <label for="">{{user.mail}}</label> <br>
        <input type="text"  placeholder="username" required ng-   
         model="user.mail"><br>
        <label for="">{{user.pass}}</label> <br>
        <input type="text"  placeholder="password" required ng-
        model="user.pass"><br>
        <button  name="submit" class="btn btn-default"  ng-click="login()">   
         Submit</button>
         </form>
        </div>
      </div>

MAIN.JS

    app.factory('loginService',function($http){
    return{
        login:function(user,scope){
            var $promise = $http.post('http://site1.local/user.php', user);   
       //send data to user.php
            $promise.then(function(msg){
                if(msg.data=='success') console.log("GJ");
                else console.log("DARN IT");
            });
        }
     }

});

app.controller('LoginCtrl', function( $scope, loginService ) {
   $scope.login=function(user){
      console.log(user);
        loginService.login(user,$scope); //call login service
    };
});

PHP

<?php 

    $data = file_get_contents("php://input");
    $user = json_decode($data);
    if($user->mail=='admin' && $user->pass=='1234') 
        print 'success';
    else 
        print 'error';

?>

1 个答案:

答案 0 :(得分:2)

在ng-click功能中传递用户

ng-click = "login(user)"