$http 是 AngularJS 应用中最常用的服务。 服务向服务器发送请求,应用响应服务器传送过来的数据。
有两种请求方式post和get
1、post请求方式;
var app=angular.module('app',[]);
//注入服务 $http 网络服务
app.controller('main',['$scope','$http',function($scope,$http){
//post ----使用$http
$http({
method:'post',
url:'php/post.php',
data:'name="张三"&psd=666',//只有post使用
headers:{
'content-type':'application/x-www-form-urlencoded'
}
}).then(function(res){//then 有两个参数 对应于 ajax里面的回调函数
console.log(res.data);
},function(){
console.log('请求错误 失败')
})
2、get请求方式:
//get
$http({
method:'get',
url:'php/get.php',
params:{
name:'小兰',
psd:'999'
}
}).then(function(res){
console.log(res.data);
},function(){
console.log('请求数据失败')
})