请看看这个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工作
发布评论