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


AngularJS $http.post() promise returns no data

这是我第一次使用angular,在花了很多时间在在线教程和指南上之后,我最终陷入了会话管理。Everithing运行良好,只是当我登录时,变量$_SESSION['uid']包含我想要的内容,但该值没有解析回$promise.then(function(response){...});,因此我无法正确启动会话。你能给我一个线索吗?

接下来的教程:angularjs和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"}
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');
            } 
        });
    },

在你回答后,我尝试了这个,因为我更了解它,而且它也很有效。

代替:

$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');
        } 
    });
}