AngularJs Post Method


AngularJs Post Method

controller

).controller('LoginController',
                         [
                           '$scope',
                            'dataService',
                            '$location',
                            '$window',
                    function ($scope, dataService, $location,$window){
                        $scope.check_login=function($event,userID,passwd)
                       {
                         dataService.login(userID,passwd).then
                        (
                        function (response){
                         $scope.loginCount = response.rowCount + 'account Record';
                         $scope.loginConfirm = response.data;
                         console.log(response.data);
                         },
                        function (err) {
                         $scope.status = 'unable to connect to data' + err;
                        });
                        // $scope.reloadRoute = function () {
                        // $location.path('/#');
                        // $window.location.reload()

                        // }//end of reload route fnction
                       }//end of function check_login 

                }
            ]

服务.js

this.login = function (userID, passwd) {
                  var defer = $q.defer(),
                    data = {
                      username: userID,
                      password: passwd
                      };
                    $http.post(urlBase, {
                              params: data,
                              headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                              cache: true
                      })
                      . // notice the dot to start the chain to success()
                    success(function (response) {
                    defer.resolve({
                    data: response.login.Result, // create data property with value from response
                    rowCount: response.login.RowCount // create rowCount property with value from response
                    });
                  })
              . // another dot to chain to error()
            error(function (err) {
              defer.reject(err);
            });
            // the call to getCourses returns this promise which is fulfilled 
            // by the .get method .success or .failure
            return defer.promise;
          };

索引.php

if (isset($_POST['username']) && isset($_POST['password'])) {
            $useremail = $_POST['username'];
            $password  = $_POST['password'];
            $service   = new FilmsService();
            $result    = $service->login($useremail, $password);
            echo $result;
        } else {
            echo "Cant Find The Data";
        }

目前我得到了3个文件名控制器服务.js索引.php,服务.js是你把数据传递给php端,但是当我尝试在php端获取用户名和密码时,它会出错。无法获取用户名和密码

如何解决? 是我的代码错误吗?

试试这个来代替你的$http.post:

$http({
 method: 'POST',
 url: urlBase,
 headers: {
   'Content-Type': 'application/x-www-form-urlencoded'
 },
 data: data
});