【发布时间】:2014-05-26 12:03:05
【问题描述】:
当我使用$http.get 时,我的代码可以工作,但如果我使用$http.post,我永远不会获得请求.php 文件的参数。
这是服务功能:
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 文件
<?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文件没有参数。
如果我使用 $.ajax 或 $.post 我的代码可以工作并给出响应。
【问题讨论】:
-
这个解决方案对我有用stackoverflow.com/questions/19254029/…
标签: javascript php jquery angularjs