AngularJs Post方法

时间:2016-03-18 16:33:54

标签: php angularjs

控制器

if(position == 0)
   // set background color for View

Services.js

).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 


                }

            ]

的index.php

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;
          };

目前我有3个文件名控制器 service.js index.php ,service.js是您将数据传递给php端,但是当我尝试在php端获取用户名密码时,它将是错误的。可以获取用户名并且密码

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

1 个答案:

答案 0 :(得分:0)

尝试使用此替代$ http.post:

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