$ http.post不是AngularJS工作

编程入门 行业动态 更新时间:2024-10-11 21:21:46
本文介绍了$ 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工作

本文发布于:2023-10-10 09:45:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478324.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   http   post   AngularJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!