我使用$http方法从
angularjs控制器向PHP页面发布一些数据,如下所示.
$scope.login = function(){ var data = { 'username' : $scope.username,'password' : $scope.password }; $http.post('login.PHP',data).success(function(response){ console.log(response); }); };
$postdata = json_decode(file_get_contents('PHP://input'),true);
但我想知道在angularjs中是否有更好的方法,所以我可以使用PHP中的简单$_POST来检索数据.
在jQuery中我们可以简单地使用$.post,这些可用于$_POST中的PHP.为什么angularjs并不那么简单.
当我们在angularjs中执行$http post请求时,有没有办法填充$_POST的PHP.请帮忙.
解决方法
您需要将您的请求转换为PHP已知格式.
我使用jQuery $.param方法来构建它.你可以自己写.
我使用jQuery $.param方法来构建它.你可以自己写.
app.config(['$httpProvider',function($http) { $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; $http.defaults.transformRequest = function(data) { return $.param(data); }; }]);
urlencoded遵循简单格式:
username=Name&password=My%20Password