When I use $http.get
my 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文件没有params。
If I use $.ajax or $.post my code work and it gives the response.
如果我使用$ .ajax或$ .post我的代码工作,它会给出响应。
4 个解决方案
#1
4
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。
#2
3
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.
我遇到了同样的问题,但我发现在角度http 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
我使用此代码从角度获取发布的数据,其工作就像一个魅力
#3
2
Post data like this:
发布这样的数据:
$http.post('/data/AJAXRequest.php', { mydata: 1, abcd: 2 })
#4
0
$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.
必须在数组的末尾添加标头,否则它将无法工作。
#1
4
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。
#2
3
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.
我遇到了同样的问题,但我发现在角度http 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
我使用此代码从角度获取发布的数据,其工作就像一个魅力
#3
2
Post data like this:
发布这样的数据:
$http.post('/data/AJAXRequest.php', { mydata: 1, abcd: 2 })
#4
0
$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.
必须在数组的末尾添加标头,否则它将无法工作。