AngularJS $http service

原文链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/angularjs-http-service/

1.通常用法:

$http服务是一个只有一个参数-配置对象的函数,用于产生一个http请求并返回一个promise对象。

$http({

method: ‘GET‘,

url: ‘/someUrl‘

}).then(function successCallback(response) {

//响应成功

}, function errorCallback(response) {

//响应失败

});

这个回复对象有这些属性:

data:转换函数转换之后的的响应体

status:响应的http状态码

header:得到的头部getter函数

config:用于生成请求的配置对象

statusText:响应的http状态文本

响应状态码(status)在200-299之间被认为是成功状态,并且会导致successCallback函数被调用。

2.快捷用法:

所有快捷用法都需要通过URL,并要求数据必须通过post或put要求。

$http.get(‘/someUrl‘, config).then(successCallback, errorCallback);

$http.post(‘/someUrl‘, data, config).then(successCallback, errorCallback);

快捷用法有:

  • $http.get获得服务器上的某一资源
  • $http.head  与get类似,但只获得http头信息
  • $http.post 向服务器提交数据、更新创建资源等
  • $http.put  用于向指定的URI传送更新资源
  • $http.delete  删除某一资源
  • $http.jsonp  获取jsonp数据
  • $http.patch  用于更新某一部分的内容

3.使用$http编写单元测试

当进行单元测试时(用ngMock),必须调用$httpbackend.flush()来清除每个待定的请求。

$httpBackend.expectGET(...);

$http.get(...);

$httpBackend.flush();

4.返回一个promise对象

var promise=$http({

method:‘GET‘,

url:"data.json"

});

promise.then(function(resp){

//返回resp响应对象

},function(resp){

//返回错误信息的resp

});

时间: 2024-08-07 04:08:04

AngularJS $http service的相关文章

Part 20 Create custom service in AngularJS

Whenever the case changes from lower to upper, a single space character should be inserted. This means the string "AngularVideoTutorial" should be converted to"Angular Video Tutorial". Let us first see, how to achieve this without usin

Part 18 $http service in AngularJS

In Angular there are several built in services. $http service is one of them. In this video, we will discuss another built in service, $log. It is also possible to create our own custom services in Angular. At this point several questions come to our

Part 17 Consuming ASP NET Web Service in AngularJS using $http

Here is what we want to do1. Create an ASP.NET Web service. This web service retrieves the data from SQL Server database table, returns it in JSON formt.2. Call the web service using AngularJS and display employee data on the web page Step 1 : Create

Service and controller in angularJs

Separation of concern is at the heart while designing an AngularJS application. Your controller must be responsible for binding model data to views using $scope. It does not contain logic to fetch the data or manipulating it. For that we must create

另类angularjs应用

回顾 上一篇文章主要讲解了创建兼容任意浏览器(主要是ie的一些奇葩问题)的angularjs web应用,但是项目开发中其实更重要的还是在功能的模块化.代码自动压缩上面,这样项目在后期维护或者功能的重复利用上才会更方便,那么今天主要围绕以下几个主题来讲讲如何在不是用其他js模块化库的情况下,开发便于管理的angualrjs模块化代码: 使用service来创建模块 模块间引用 代码合并及压缩 本文中的代码均已nodejs来实现. nodejs中,使用UglifyJS2来实现代码压缩 使用serv

Getting Started with Django Rest Framework and AngularJS

转载自:http://blog.kevinastone.com/getting-started-with-django-rest-framework-and-angularjs.html A ReSTful API is becoming a standard component of any modern web application. The Django Rest Framework is powerful framework for developing ReST endpoints

mysqld服务启动失败, Failed to restart mysqld.service: Unit not found.

-bash-4.2# service mysqld restart Redirecting to /bin/systemctl restart mysqld.serviceFailed to restart mysqld.service: Unit not found. 并不存在 mysqld 的服务, -bash-4.2# -bash-4.2# chkconfig -list -list: unknown option -bash-4.2# chkconfig --list Note: Thi

Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]56528192: startup date [Tue Sep 19 15:05:24 CST 2017]; root of context hierarchy 2017-09-19 15:05:24.858 INFO 9986 --

开发者为何对Service Fabric爱不释手?值得关注!

有了它,人人都可开发高可用高伸缩应用.今天小编就为大家介绍一款开发者的"利器"--Service Fabric . 在介绍它之前,先来了解一下它的背景. Service Fabric 是一款应用程序平台,可用于构建基于微服务的应用程序.其核心部分是一个分布式系统平台,用于构建可扩展的可靠应用.在便于封装可部署代码的同时,还内置了微服务最佳实践案例. 快速上市:通过 Service Fabric,开发人员可将重点放在创建可为应用程序增加商业价值的功能上,从而避免了为在基础结构中处理可靠性

在GlassFish应用服务器上创建并运行你的第一个Restful Web Service【翻译】

前言 本人一直开发Android应用,目前Android就业形势恶劣,甚至会一路下滑,因此决定学习服务器开发.采用的语言是java,IDE是Intellij,在下载Intellij的同时看到官网很多优秀的guide文章,于是按照guide成功完成了一个RESTful的demo.官方文档非常简洁,给我带来了很大的帮助,于是翻译之,希望对其他不愿意看原文的人有所帮助.由于水平有限,读者发现错误请指正,谢谢. 原文地址: https://www.jetbrains.com/help/idea/2016