
1 <!DOCTYPE html>
2 <html ng-app="myApp">
3 <head lang="en">
4 <meta charset="UTF-8">
5 <title>HTTP对象的方法请求服务端API-post方式</title>
6 <script src="js/angular.js" type="text/javascript"></script>
7
8 </head>
9 <body>
10 <div ng-controller="myCtrl">
11 <input type="text" ng-model="num"/>
12
13 <div>{{result}}</div>
14 <button ng-click="send()">请求</button>
15 </div>
16
17 </body>
18
19 <script type="text/javascript">
20 angular.module("myApp", [])
21 .config(function ($httpProvider) {
22 $httpProvider.defaults.headers.post = {
23 'Content-Type': 'application/x-www-form-urlencoded'//头文件请求类型 表单方式传数据
24 }
25 $httpProvider.defaults.transformRequest =
26 function (obj) {
27 var arrStr = [];//字符对象的格式传送数据
28 for (var p in obj) {
29 arrStr.push(encodeURIComponent(obj[p]));
30 }
31 return arrStr.join("&");
32 }
33 })
34 .controller("myCtrl", ["$scope", "$http", function ($scope, $http) {
35 $scope.num = "";
36 $scope.result = "";
37 $scope.send = function () {
38 $http({
39 method: 'POST',
40 url: 'data/chk.php',
41 data: {
42 n: $scope.num
43 }
44 }
45 )
46 .
47 success(function (data, status, headers, config) {
48 $scope.result = data;
49 //console.log(status+"/"+headers+"/"+config);
50 })
51 }
52
53
54 }]);
55
56
57 /*服务端文件data/chk.php
58 <?php
59 function checkNum($num){
60 return ($num%2) ? TRUE : FALSE;
61 }
62 $num=$_POST["n"];
63 if(checkNum($num)===TRUE){
64 echo "奇数";
65 }else{
66 echo "偶数";
67 }
68 ?>**/
69
70 </script>
71
72 </html>
<!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
<meta charset="UTF-8">
<title>HTTP对象的方法请求服务端API-post方式</title>
<script src="js/angular.js" type="text/javascript"></script>
</head>
<body>
<div ng-controller="myCtrl">
<input type="text" ng-model="num"/>
<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.num = "";
$scope.result = "";
$scope.send = function () {
$http({
method: 'POST',
url: 'data/chk.php',
data: {
n: $scope.num
}
}
)
.
success(function (data, status, headers, config) {
$scope.result = data;
//console.log(status+"/"+headers+"/"+config);
})
}
}]);
/*服务端文件data/chk.php
<?php
function checkNum($num){
return ($num%2) ? TRUE : FALSE;
}
$num=$_POST["n"];
if(checkNum($num)===TRUE){
echo "奇数";
}else{
echo "偶数";
}
?>**/
</script>
</html>