33.http-post

 1 <!DOCTYPE html>
 2 <html ng-app="myApp">
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title>HTTP-POST</title>
 6     <script src="js/angular.js" type="text/javascript"></script>
 7 
 8 </head>
 9 <body>
10 <div ng-controller="myCtrl">
11     <div>{{result}}</div>
12     <button ng-click="send()">请求</button>
13 </div>
14 
15 </body>
16 
17 <script type="text/javascript">
18     angular.module("myApp", [])
19             .config(function ($httpProvider) {
20                 $httpProvider.defaults.headers.post = {
21                     'Content-Type': 'application/x-www-form-urlencoded'//头文件请求类型 表单方式传数据
22                 }
23                 $httpProvider.defaults.transformRequest =
24                         function (obj) {
25                             var arrStr = [];//字符对象的格式传送数据
26                             for (var p in obj) {
27                                 arrStr.push(encodeURIComponent(obj[p]));
28                             }
29                             return arrStr.join("&");
30                         }
31             })
32             .controller("myCtrl", ["$scope", "$http", function ($scope, $http) {
33                 $scope.result = "";
34                 $scope.send = function () {
35                     $http.post('data/post.php', {"name": "陶国荣"})
36                             .success(function (data, status, headers, config) {
37                                 $scope.result = data;
38                             })
39                 }
40 
41 
42             }])
43 
44 
45     /*服务端文件data/post.php
46      <?php
47      $str=$_POST["name"];
48      if($str=="陶国荣"){
49      echo "验证正确!";
50      }else{
51      echo "验证失败!"
52      }
53      ?>**/
54 
55 </script>
56 
57 </html>
View Code 1
<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
<meta charset="UTF-8">
<title>HTTP-POST</title>
<script src="js/angular.js" type="text/javascript"></script>

</head>
<body>
<div ng-controller="myCtrl">
<div>{{result}}</div>
<button ng-click="send()">请求</button>
</div>

</body>

<script type="text/javascript">
angular.module("myApp", [])
.config(function ($httpProvider) {
$httpProvider.defaults.headers.post = {
'Content-Type': 'application/x-www-form-urlencoded'//头文件请求类型 表单方式传数据
}
$httpProvider.defaults.transformRequest =
function (obj) {
var arrStr = [];//字符对象的格式传送数据
for (var p in obj) {
arrStr.push(encodeURIComponent(obj[p]));
}
return arrStr.join("&");
}
})
.controller("myCtrl", ["$scope", "$http", function ($scope, $http) {
$scope.result = "";
$scope.send = function () {
$http.post('data/post.php', {"name": "陶国荣"})
.success(function (data, status, headers, config) {
$scope.result = data;
})
}


}])


/*服务端文件data/post.php
<?php
$str=$_POST["name"];
if($str=="陶国荣"){
echo "验证正确!";
}else{
echo "验证失败!"
}
?>**/

</script>

</html>
原文地址:https://www.cnblogs.com/mx2036/p/6924475.html