AngularJS HTTP发布到PHP
发布时间:2020-12-17 17:50:13 所属栏目:安全 来源:网络整理
导读:我使用$http方法从 angularjs控制器向php页面发布一些数据,如下所示. $scope.login = function(){ var data = { 'username' : $scope.username,'password' : $scope.password }; $http.post('login.php',data).success(function(response){ console.log(resp
我使用$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); }); }; 我知道我可以使用以下方法在php中检索此数据: $postdata = json_decode(file_get_contents('php://input'),true); 但我想知道在angularjs中是否有更好的方法,所以我可以使用php中的简单$_POST来检索数据. 在jQuery中我们可以简单地使用$.post,这些可用于$_POST中的php.为什么angularjs并不那么简单. 解决方法
您需要将您的请求转换为php已知格式.
我使用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 JSFiddle (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |