javascript $http.post 在 AngularJS 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23027721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:12:11  来源:igfitidea点击:

$http.post not working in AngularJS

javascriptphpjqueryangularjs

提问by Ashish Panwar

When I use $http.getmy code works, but if I use $http.post, I never get the parameters to the request .php file.

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

This is Service function:

这是服务功能:

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

And Controller function:

和控制器功能:

 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();
    }
]);

and My AJAXRequest.php file

和我的 AJAXRequest.php 文件

<?php 
   var_dump($_POST)
?>

if I use $http.post()

如果我使用$http.post()

Output:

输出

 array (size=0)
  empty

If i use $http.get()my output is :

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

array (size=2)
  'mydata' => string '1' (length=1)
  'abcd' => string '2' (length=1)

I checked the post in FireBug tool, that its sending data to my php file. but php file getting no params.

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

If I use $.ajaxor $.postmy code work and it gives the response.

如果我使用$.ajax$.post我的代码工作并给出响应。

采纳答案by Ryan Taylor

What if you specify the content-type in the headers, specifically like this:

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

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

Found comments relating to PHP specifically from this question: AngularJs $http.post() does not send data

从这个问题中找到了与 PHP 相关的评论:AngularJs $http.post() 不发送数据

It would seem that Angular sends as application/json by default, which can confuse PHP.

默认情况下,Angular 似乎以 application/json 的形式发送,这可能会混淆 PHP。

回答by Harry Matharoo

I faced the same issue but I found that there is no issue in angular http post method, issue is there where I am trying to get post data.

我遇到了同样的问题,但我发现 angular http post 方法没有问题,问题是我试图获取 post 数据的地方。

$params = json_decode(file_get_contents('php://input'),true);

I used this code to get the posted data from angular and its works like a charm

我使用此代码从 angular 获取发布的数据,它的工作原理非常棒

回答by karaxuna

Post data like this:

像这样发布数据:

$http.post('/data/AJAXRequest.php', { mydata: 1, abcd: 2 })

回答by Dev.Jitendra

$http({
    method: 'POST',
    url: 'http://mtapi.azurewebsites.net/api/sectiontype',
    dataType: 'json',
    data: {SectionTypeId:0, Name: $scope.message},
    headers: { 'Content-Type': 'application/json; charset=UTF-8' }
}).success(function (data) {
    alert(data);
}).error(function (data) {
    alert(data);
});

headers must be added in the end of the array otherwise it won't work.

必须在数组的末尾添加标头,否则将无法工作。