AngularJS $ http.post()promise不返回任何数据

时间:2016-03-27 13:22:45

标签: php angularjs

这是我第一次使用角度,经过大量时间花在在线教程和指南上后,我最终陷入了会话管理。 Everithing工作正常,但是当我进行登录时,变量$_SESSION['uid']包含我想要的内容,但该值不会被解析回$promise.then(function(response){...});,因此我无法正常启动会话。 你能给我一个线索吗?

随后的教程:Simple session with angularjs and php, angularJs

user.php的

<?php 
$json =file_get_contents('php://input');
$user=json_decode($json);  //get user from JSON


if($user->mail=='email@gmail.com' && $user->pass=='1234') 
    session_start();
    //$_SESSION['uid'] = uniqid('ang_');
    $_SESSION['uid'] = $user->mail;
    return $_SESSION['uid'];           ?>

loginService.js

(function() {
'use strict';
  var app = angular.module('loginS',['sessionS']);

  app.factory('loginService',['$http','$location','sessionService',function($http,$location,sessionService){
    return{
        login:function(data,scope){
            var $promise = $http.post('data/user.php', data);// send data to user.php
            $promise.then(function(response){
                console.log(response);
                var uid = response.data;
                if(uid!=""){
                    //scope.msgtext='Correct information';
                    sessionService.set('uid',uid);
                    $location.path('/home');

                }
                else{
                    scope.msgtext='Wrong information';
                    $location.path('/login');
                } 

            });
        },
        logout:function(){
            sessionService.destroy('uid');
            $location.path('/login');
        },

        isLogged:function(){
            var $checkSessionServer =$http.post('data/check_session.php');
            return $checkSessionServer;
            /*
            if(sessionService.get('user')) return true; 
            else return false;
            */
        }
    }
  }]);
})();

login.tmp.html

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

这是我从承诺中获得的......我无法弄清楚为什么......

console.log

Object {data: "", status: 200, config: Object, statusText: "OK"}

2 个答案:

答案 0 :(得分:1)

php中的

return不会向浏览器输出任何内容....您需要使用echo

另外,$http的默认预期内容类型是json,所以我会更改:

return $_SESSION['uid']; 

类似于:

$output = array('uid'=>$_SESSION['uid']);

echo json_encode($output);

然后在角度:

login:function(data,scope){
        var $promise = $http.post('data/user.php', data);// send data to user.php
        $promise.then(function(response){
            console.log(response);
            var uid = response.data.uid;
            if(uid){
                //scope.msgtext='Correct information';
                sessionService.set('uid',uid);
                $location.path('/home');

            }
            else{
                scope.msgtext='Wrong information';
                $location.path('/login');
            } 

        });
    },

答案 1 :(得分:0)

在你回答之后我尝试过这个因为我理解得更好而且也有效。

而不是:

$output = array('uid'=>$_SESSION['uid']);

echo json_encode($output);

我用过这个:

$_SESSION['uid'] = $user->mail;
echo $_SESSION['uid'];

所以在Angular:

login:function(data,scope){
    var $promise = $http.post('data/user.php', data);// send data to user.php
    $promise.then(function(response){
        console.log(response);
        var uid = response.data;
        if(uid!=""){
            //scope.msgtext='Correct information';
            sessionService.set('uid',uid);
            $location.path('/home');

        }
        else{
            scope.msgtext='Wrong information';
            $location.path('/login');
        } 

    });
}