$ http.post在AngularJS中不起作用

Ash*_*war 8 javascript php jquery angularjs

当我使用$http.get我的代码工作,但如果我使用$ http.post,我永远不会得到请求.php文件的参数.

这是服务功能:

    TestPanel.service('MySampleService', function ($http, $q) {
    this.getAllPosts = function () {       

        var def = $q.defer();
        $http.post('/data/AJAXRequest.php', 'mydata=1&abcd=2').success(function (data) {
            if (data == null)
                def.reject('ERROR: DATA IS NULL');
            else if (data.HasError)
                def.reject('ERROR: ' + data.Message);
            else
                def.resolve(data);
        }).error(function () {
            def.reject('ERROR: Sorry, unable to complete your request.');
        });

        return def.promise;
    }
});
Run Code Online (Sandbox Code Playgroud)

和控制器功能:

 TestController.controller('PostCtrl', ['$scope', '$http', 'MySampleService',
    function ($scope, $http, MySampleService) {       

        function FetchPostsList() {
            MySampleService.getAllPosts().then(function (data) {
                $scope.lstPosts = data.ResponseData;
                $scope.totalRecords = data.totalRecords;
                console.info('DATA=' + $scope.lstPosts);
            },
            function (err) {
                console.info('err=' + err);
            });
        }

        FetchPostsList();
    }
]);
Run Code Online (Sandbox Code Playgroud)

和我的AJAXRequest.php文件

<?php 
   var_dump($_POST)
?>
Run Code Online (Sandbox Code Playgroud)

如果我使用$ http.post()

输出:

 array (size=0)
  empty
Run Code Online (Sandbox Code Playgroud)

如果我使用$ http.get() 我的输出是:

array (size=2)
  'mydata' => string '1' (length=1)
  'abcd' => string '2' (length=1)
Run Code Online (Sandbox Code Playgroud)

我检查了FireBug工具中的帖子,它将数据发送到我的php文件.但php文件没有params.

如果我使用$ .ajax$ .post我的代码工作,它会给出响应.

Rya*_*lor 4

如果您在标头中指定内容类型,具体如下:

$http({
method: 'POST',
url: '/data/AJAXRequest.php',
data: { mydata: 1, abcd: 2 },
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(....);
Run Code Online (Sandbox Code Playgroud)

从这个问题中专门找到了与 PHP 相关的评论:AngularJs $http.post() does not send data

Angular 默认情况下以 application/json 形式发送,这可能会让 PHP 感到困惑。