2018-05-17 11:09:36 509浏览
我们可以使用内置的$http服务直接同外部进行通信。$http服务只是简单的封装了浏览器原生的XMLHttpRequest对象。
$http({
url:'data.json',
method:'GET'
}).success(function(data,header,config,status){
//响应成功
}).error(function(data,header,config,status){
//处理响应失败
});
varpromise=$http({
method:'GET',
url:"data.json"
});
promise.then(function(resp){
//resp是一个响应对象
},function(resp){
//带有错误信息的resp
});
或者这样:
promise.success(function(data,status,config,headers){
//处理成功的响应
});
promise.error(function(data,status,hedaers,config){
//处理失败后的响应
});
varpromise=$http.jsonp("/api/users.json?callback=JSON_CALLBACK");
$http({
method:'GET',
url:'/api/users.json',
params:{
'username':'tan'
});
//参数会转为?name=ari的形式
$http({
params:{'name':'ari'}
});
varblob=newBlob(['Helloworld'],{type:'text/plain'});
$http({
method:'POST',
url:'/',
data:blob
});
$http({
method:'GET',
url:'/api/users.json'
}).then(resp){
//读取X-Auth-ID
resp.headers('X-Auth-ID');
});
$http.get('/api/users.json',{cache:true})
.success(function(data){})
.error(function(data){});
以上就是扣丁学堂HTML5培训浅谈AngularJS中$http服务用法的全部内容,想要了解更多html5相关内容的小伙伴可以登录扣丁学堂官网查询,扣丁学堂不仅有专业的老师和与时俱进的课程体系,还有大量的html5视频教程供学员观看学习,想要学习html5的小伙伴快选择专业的html5在线学习机构扣丁学堂学习吧。扣丁学堂H5技术交流群:559883758。
【关注微信公众号获取更多学习资料】