$http服务-post请求
1.post注意:
1.post必须设置请求头 headers:
2.post 请求默认是以json
Content-Type:application/json;charset=UTF-8
3.以php为例,post接收formData , 是key:value 格式数据
4.所以不加请求头, 以默认Jason格式接收数据,会发生错误
5.所以需要修改请求头为 以下格式:
'Content-Type':'application/x-www-form-urlencoded'6.data: 默认请求参数也是以 json/formData ->resful/soap
正确格式: 'flag=xmg'
-
[post请求默认数据格式]
-
[修改请求头后 formData格式]
-
[最终formData格式]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body ng-app="app" ng-controller="skController">
<p>{{res}}</p>
</body>
<script src="angular.js"></script>
<script>
//1.创建模块
var app = angular.module('app', []);
//2.创建控制器
app.controller('skController', ['$scope', '$http',function ($scope, $http) {
//$scope.res = 'hello world';
$http({
url:'post.php',
method:'post',
headers:{
//'Content-Type':'application/json;charset=UTF-8'
'Content-Type':'application/x-www-form-urlencoded'
},
/* data:{
flag:'xmg'
}*/
data:'flag=xmg'
}).success(function (res) {
$scope.res = res;
}).error(function (error) {
console.log(error);
})
}]);
//3.绑定模块 ng-app='app'
//4.绑定控制器
</script>
</html>