$ http.post不是AngularJS工作 [英] $http.post not working in AngularJS

查看:121
本文介绍了$ http.post不是AngularJS工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看看这个code,告诉我什么即时通讯做错了。当我用$ http.get我的code的工作,但如果我使用$ http.post我从来没有得到的参数要求的.php文件。

Please look at this code and tell me what im doing wrong. When i use $http.get my code work, but if i use $http.post i never get the parameters to the request .php file.

这是服务功能:

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

和控制功能:

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


我AJAXRequest.php文件

and My AJAXRequest.php file

<?php 
   var_dump($_POST)
?>

如果我使用 $ http.post()

输出

 array (size=0)
  empty

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

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

我检查在Firebug工具后,其将数据发送到我的PHP文件。但PHP文件没有得到PARAMS。

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

如果我使用 $。阿贾克斯 $。POST 我的code的工作,它给了回应。

If i use $.ajax or $.post my code work and it gives the response.

请帮我在这。结果
感谢:)

Please help me in this.
Thanks :)

推荐答案

如果您在头指定的内容类型是什么,具体是这样的:

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

从这个问题涉及到PHP专门找到评论: AngularJs $ http.post ()不发送数据

这似乎角发送为application /默认JSON,它可以混淆PHP。

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

这篇关于$ http.post不是AngularJS工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆