SlideShare a Scribd company logo
Angular server-side communication
Angular server-side communication
core Angular Service for communcitation with remote 
HTTP servers; 
can communicate via the browser's 
or via 
works hand in hand with $q service.
$http({method: 'GET', url: '/someUrl'}) 
.success(function(data, status, headers, config) { 
// this callback will be called asynchronously 
// when the response is available 
}) 
.error(function(data, status, headers, config) { 
// called asynchronously if an error occurs 
// or server returns response with an error status. 
}); 
*Since the returned value is a , you can also use 
for registering callbacks.
$http.get 
$http.head 
$http.post 
$http.put 
$http.delete 
$http.jsonp 
$http.patch
Angular server-side communication
Angular server-side communication
Angular server-side communication
The $http service will automatically add certain HTTP headers to 
all requests. 
The default headers can be fully configured by accesing the 
configuration object which contains: 
(headers that are common for all requests) 
(header defaults for POST requests) 
(header defaults for PUT requests) 
(header defaults for PATCH requests)
Angular server-side communication
//Add a new function at the end of chain 
$httpProvider.defaults.transformResponse.push(function(data, headersGetter){ 
//data it's already converted from an angular default transformer 
data.total = headersGetter('X-Total-Count'); 
return data; 
}); 
//Replace the entire chain 
$httpProvider.defaults.transformResponse = [function(data, headersGetter){ 
//assume the data is a JSON string 
var tranformedData = angular.fromJson(data); 
tranformedData.total = headersGetter('X-Total-Count'); 
return tranformedData; 
}];
Angular server-side communication
Angular server-side communication
There are two kinds of interceptors (and two kinds of rejection interceptors): 
: interceptors get called with a http config object. The function is 
free to modify the config object or create a new one. The function needs to 
return the config object directly, or a promise containing the config or a 
new config object. 
: interceptor gets called when a previous interceptor threw an 
error or resolved with a rejection. 
: interceptors get called with http response object. The function is 
free to modify the response object or create a new one. The function needs 
to return the response object directly, or as a promise containing the 
response or a new response object. 
: interceptor gets called when a previous interceptor threw 
an error or resolved with a rejection.
// register the interceptor as a service 
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) { 
return { 
// optional method 
'request': function(config) { 
// do something on success 
return config; 
}, 
// optional method 
'requestError': function(rejection) { 
// do something on error 
if (canRecover(rejection)) { 
return responseOrNewPromise 
} 
return $q.reject(rejection); 
}, 
// optional method 
'response': function(response) { 
// do something on success 
return response; 
}, 
// optional method 
'responseError': function(rejection) { 
// do something on error 
if (canRecover(rejection)) { 
return responseOrNewPromise 
} 
return $q.reject(rejection); 
} 
}; 
}); 
$httpProvider.interceptors.push('myHttpInterceptor');
Angular server-side communication
Usage: 
//Resource Constructor provided by $resource factory 
var User = $resource('/user/:id', {id:'@id'}); 
//new Resource instance 
var user = new User({name: 'John'}); 
//Prototype methods from Resource class - persist to backend 
user.$save().then(function(){ 
//Delete the user 
user.$delete(); 
}); 
//Get existing user 
var user2 = User.get({id: 2}, function(){ 
user.name = 'Another name'; 
user.$save(); 
});
- A parametrized URL template with parameters 
prefixed by as in 
- Given a template and 
parameter results in 
URL . 
- Hash with declaration of custom action that 
should extend the default set of resource actions. 
action: {method: '', params:{}, isArray: false}
Angular server-side communication
The default set of resource actions optionally extended with custom actions. 
Default actions: 
query : {method: 'GET'} 
get: {method: 'GET'} 
save: {method: 'POST'} 
remove: {method: 'DELETE'} 
delete: {method: 'DELETE'} 
Invocation: 
HTTP GET "class" actions: 
non-GET "class" actions:
Resource instances are instances created by invocation of the 
constructor with new operator, or by invocation of constructor 
methods 
The instance level-methods are prefixed with the sign 
Instance methods have equivalent functionality with 
constructor's methods 
Instance method are available only for non-GET operations
Plunker Example
Angular Api Reference

More Related Content

PDF
Angular promises and http
Alexe Bogdan
 
PPTX
Avoiding callback hell in Node js using promises
Ankit Agarwal
 
PDF
Rntb20200805
t k
 
PDF
React native-firebase startup-mtup
t k
 
PDF
JavaScript Promise
Joseph Chiang
 
PPTX
JavaScript Sprachraum
patricklee
 
PPTX
AngularJS - $http & $resource Services
Eyal Vardi
 
PDF
Workshop 23: ReactJS, React & Redux testing
Visual Engineering
 
Angular promises and http
Alexe Bogdan
 
Avoiding callback hell in Node js using promises
Ankit Agarwal
 
Rntb20200805
t k
 
React native-firebase startup-mtup
t k
 
JavaScript Promise
Joseph Chiang
 
JavaScript Sprachraum
patricklee
 
AngularJS - $http & $resource Services
Eyal Vardi
 
Workshop 23: ReactJS, React & Redux testing
Visual Engineering
 

What's hot (20)

DOCX
VPN Access Runbook
Taha Shakeel
 
PDF
Asynchronous Programming FTW! 2 (with AnyEvent)
xSawyer
 
PDF
Workshop 5: JavaScript testing
Visual Engineering
 
PPTX
JavaScript Promises
L&T Technology Services Limited
 
PDF
Callbacks, promises, generators - asynchronous javascript
Łukasz Kużyński
 
PDF
Reduxing like a pro
Boris Dinkevich
 
PPTX
AngularJS $http Interceptors (Explanation and Examples)
Brian Swartzfager
 
DOCX
exportDisabledUsersRemoveMailbox
Daniel Gilhousen
 
KEY
Cakephp2study tips集
Kohji Tanaka
 
PDF
Asynchronous programming patterns in Perl
deepfountainconsulting
 
PPTX
Angular 1.x vs. Angular 2.x
Eyal Vardi
 
PDF
Boosting Angular runtime performance
Nir Kaufman
 
PDF
JavaScript Promises
Tomasz Bak
 
PDF
Mirage For Beginners
Wilson Su
 
PPTX
Angular 2 Architecture
Eyal Vardi
 
PDF
Why Redux-Observable?
Anna Su
 
PPTX
Template syntax in Angular 2.0
Eyal Vardi
 
PDF
Présentation de HomeKit
CocoaHeads France
 
PDF
Redux Sagas - React Alicante
Ignacio Martín
 
PDF
Introduction to ReactJS and Redux
Boris Dinkevich
 
VPN Access Runbook
Taha Shakeel
 
Asynchronous Programming FTW! 2 (with AnyEvent)
xSawyer
 
Workshop 5: JavaScript testing
Visual Engineering
 
JavaScript Promises
L&T Technology Services Limited
 
Callbacks, promises, generators - asynchronous javascript
Łukasz Kużyński
 
Reduxing like a pro
Boris Dinkevich
 
AngularJS $http Interceptors (Explanation and Examples)
Brian Swartzfager
 
exportDisabledUsersRemoveMailbox
Daniel Gilhousen
 
Cakephp2study tips集
Kohji Tanaka
 
Asynchronous programming patterns in Perl
deepfountainconsulting
 
Angular 1.x vs. Angular 2.x
Eyal Vardi
 
Boosting Angular runtime performance
Nir Kaufman
 
JavaScript Promises
Tomasz Bak
 
Mirage For Beginners
Wilson Su
 
Angular 2 Architecture
Eyal Vardi
 
Why Redux-Observable?
Anna Su
 
Template syntax in Angular 2.0
Eyal Vardi
 
Présentation de HomeKit
CocoaHeads France
 
Redux Sagas - React Alicante
Ignacio Martín
 
Introduction to ReactJS and Redux
Boris Dinkevich
 
Ad

Similar to Angular server-side communication (20)

PDF
What's new in jQuery 1.5
Martin Kleppe
 
PPT
Easy rest service using PHP reflection api
Matthieu Aubry
 
PDF
Bootstrat REST APIs with Laravel 5
Elena Kolevska
 
PPT
Spring data iii
명철 강
 
PPTX
Angular Tutorial Freshers and Experienced
rajkamaltibacademy
 
PDF
Django Class-based views (Slovenian)
Luka Zakrajšek
 
PDF
Building Lithium Apps
Nate Abele
 
PDF
Service worker: discover the next web game changer
Sandro Paganotti
 
PDF
Silex Cheat Sheet
Andréia Bohner
 
PDF
Silex Cheat Sheet
Andréia Bohner
 
DOCX
Spring 3.0
Ved Prakash Gupta
 
PDF
REST in AngularJS
a_sharif
 
PDF
ngMess: AngularJS Dependency Injection
Dzmitry Ivashutsin
 
PDF
The state of hooking into Drupal - DrupalCon Dublin
Nida Ismail Shah
 
PDF
Migrating to dependency injection
Josh Adell
 
KEY
Phpne august-2012-symfony-components-friends
Michael Peacock
 
PPTX
AngularJS Services
Eyal Vardi
 
PPT
Jsp/Servlet
Sunil OS
 
PPTX
AngularJs-training
Pratchaya Suputsopon
 
ODP
Aura Project for PHP
Hari K T
 
What's new in jQuery 1.5
Martin Kleppe
 
Easy rest service using PHP reflection api
Matthieu Aubry
 
Bootstrat REST APIs with Laravel 5
Elena Kolevska
 
Spring data iii
명철 강
 
Angular Tutorial Freshers and Experienced
rajkamaltibacademy
 
Django Class-based views (Slovenian)
Luka Zakrajšek
 
Building Lithium Apps
Nate Abele
 
Service worker: discover the next web game changer
Sandro Paganotti
 
Silex Cheat Sheet
Andréia Bohner
 
Silex Cheat Sheet
Andréia Bohner
 
Spring 3.0
Ved Prakash Gupta
 
REST in AngularJS
a_sharif
 
ngMess: AngularJS Dependency Injection
Dzmitry Ivashutsin
 
The state of hooking into Drupal - DrupalCon Dublin
Nida Ismail Shah
 
Migrating to dependency injection
Josh Adell
 
Phpne august-2012-symfony-components-friends
Michael Peacock
 
AngularJS Services
Eyal Vardi
 
Jsp/Servlet
Sunil OS
 
AngularJs-training
Pratchaya Suputsopon
 
Aura Project for PHP
Hari K T
 
Ad

More from Alexe Bogdan (7)

PDF
Dependency Injection pattern in Angular
Alexe Bogdan
 
PDF
Client Side MVC & Angular
Alexe Bogdan
 
PDF
HTML & JavaScript Introduction
Alexe Bogdan
 
PDF
Angular custom directives
Alexe Bogdan
 
PDF
Angular Promises and Advanced Routing
Alexe Bogdan
 
PDF
AngularJS - dependency injection
Alexe Bogdan
 
PDF
AngularJS - introduction & how it works?
Alexe Bogdan
 
Dependency Injection pattern in Angular
Alexe Bogdan
 
Client Side MVC & Angular
Alexe Bogdan
 
HTML & JavaScript Introduction
Alexe Bogdan
 
Angular custom directives
Alexe Bogdan
 
Angular Promises and Advanced Routing
Alexe Bogdan
 
AngularJS - dependency injection
Alexe Bogdan
 
AngularJS - introduction & how it works?
Alexe Bogdan
 

Recently uploaded (20)

PPTX
SEO Trends in 2025 | B3AITS - Bow & 3 Arrows IT Solutions
B3AITS - Bow & 3 Arrows IT Solutions
 
PPTX
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
PPTX
ENCOR_Chapter_11 - ‌BGP implementation.pptx
nshg93
 
PPT
Introduction to dns domain name syst.ppt
MUHAMMADKAVISHSHABAN
 
PDF
LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1
LABUAN 4D
 
PDF
Data Protection & Resilience in Focus.pdf
AmyPoblete3
 
PPTX
Crypto Recovery California Services.pptx
lionsgate network
 
PPTX
Microsoft PowerPoint Student PPT slides.pptx
Garleys Putin
 
PDF
Generative AI Foundations: AI Skills for the Future of Work
hemal sharma
 
PPTX
Pengenalan perangkat Jaringan komputer pada teknik jaringan komputer dan tele...
Prayudha3
 
PDF
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 
PDF
Project English Paja Jara Alejandro.jpdf
AlejandroAlonsoPajaJ
 
PPTX
how many elements are less than or equal to a mid value and adjusts the searc...
kokiyon104
 
PDF
PDF document: World Game (s) Great Redesign.pdf
Steven McGee
 
PDF
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
PPTX
B2B_Ecommerce_Internship_Simranpreet.pptx
LipakshiJindal
 
PPTX
EthicalHack{aksdladlsfsamnookfmnakoasjd}.pptx
dagarabull
 
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
APNIC
 
PDF
LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1
LABUAN 4D
 
PPTX
ENCOR_Chapter_10 - OSPFv3 Attribution.pptx
nshg93
 
SEO Trends in 2025 | B3AITS - Bow & 3 Arrows IT Solutions
B3AITS - Bow & 3 Arrows IT Solutions
 
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
ENCOR_Chapter_11 - ‌BGP implementation.pptx
nshg93
 
Introduction to dns domain name syst.ppt
MUHAMMADKAVISHSHABAN
 
LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1
LABUAN 4D
 
Data Protection & Resilience in Focus.pdf
AmyPoblete3
 
Crypto Recovery California Services.pptx
lionsgate network
 
Microsoft PowerPoint Student PPT slides.pptx
Garleys Putin
 
Generative AI Foundations: AI Skills for the Future of Work
hemal sharma
 
Pengenalan perangkat Jaringan komputer pada teknik jaringan komputer dan tele...
Prayudha3
 
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 
Project English Paja Jara Alejandro.jpdf
AlejandroAlonsoPajaJ
 
how many elements are less than or equal to a mid value and adjusts the searc...
kokiyon104
 
PDF document: World Game (s) Great Redesign.pdf
Steven McGee
 
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
B2B_Ecommerce_Internship_Simranpreet.pptx
LipakshiJindal
 
EthicalHack{aksdladlsfsamnookfmnakoasjd}.pptx
dagarabull
 
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
APNIC
 
LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1
LABUAN 4D
 
ENCOR_Chapter_10 - OSPFv3 Attribution.pptx
nshg93
 

Angular server-side communication

  • 3. core Angular Service for communcitation with remote HTTP servers; can communicate via the browser's or via works hand in hand with $q service.
  • 4. $http({method: 'GET', url: '/someUrl'}) .success(function(data, status, headers, config) { // this callback will be called asynchronously // when the response is available }) .error(function(data, status, headers, config) { // called asynchronously if an error occurs // or server returns response with an error status. }); *Since the returned value is a , you can also use for registering callbacks.
  • 5. $http.get $http.head $http.post $http.put $http.delete $http.jsonp $http.patch
  • 9. The $http service will automatically add certain HTTP headers to all requests. The default headers can be fully configured by accesing the configuration object which contains: (headers that are common for all requests) (header defaults for POST requests) (header defaults for PUT requests) (header defaults for PATCH requests)
  • 11. //Add a new function at the end of chain $httpProvider.defaults.transformResponse.push(function(data, headersGetter){ //data it's already converted from an angular default transformer data.total = headersGetter('X-Total-Count'); return data; }); //Replace the entire chain $httpProvider.defaults.transformResponse = [function(data, headersGetter){ //assume the data is a JSON string var tranformedData = angular.fromJson(data); tranformedData.total = headersGetter('X-Total-Count'); return tranformedData; }];
  • 14. There are two kinds of interceptors (and two kinds of rejection interceptors): : interceptors get called with a http config object. The function is free to modify the config object or create a new one. The function needs to return the config object directly, or a promise containing the config or a new config object. : interceptor gets called when a previous interceptor threw an error or resolved with a rejection. : interceptors get called with http response object. The function is free to modify the response object or create a new one. The function needs to return the response object directly, or as a promise containing the response or a new response object. : interceptor gets called when a previous interceptor threw an error or resolved with a rejection.
  • 15. // register the interceptor as a service $provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) { return { // optional method 'request': function(config) { // do something on success return config; }, // optional method 'requestError': function(rejection) { // do something on error if (canRecover(rejection)) { return responseOrNewPromise } return $q.reject(rejection); }, // optional method 'response': function(response) { // do something on success return response; }, // optional method 'responseError': function(rejection) { // do something on error if (canRecover(rejection)) { return responseOrNewPromise } return $q.reject(rejection); } }; }); $httpProvider.interceptors.push('myHttpInterceptor');
  • 17. Usage: //Resource Constructor provided by $resource factory var User = $resource('/user/:id', {id:'@id'}); //new Resource instance var user = new User({name: 'John'}); //Prototype methods from Resource class - persist to backend user.$save().then(function(){ //Delete the user user.$delete(); }); //Get existing user var user2 = User.get({id: 2}, function(){ user.name = 'Another name'; user.$save(); });
  • 18. - A parametrized URL template with parameters prefixed by as in - Given a template and parameter results in URL . - Hash with declaration of custom action that should extend the default set of resource actions. action: {method: '', params:{}, isArray: false}
  • 20. The default set of resource actions optionally extended with custom actions. Default actions: query : {method: 'GET'} get: {method: 'GET'} save: {method: 'POST'} remove: {method: 'DELETE'} delete: {method: 'DELETE'} Invocation: HTTP GET "class" actions: non-GET "class" actions:
  • 21. Resource instances are instances created by invocation of the constructor with new operator, or by invocation of constructor methods The instance level-methods are prefixed with the sign Instance methods have equivalent functionality with constructor's methods Instance method are available only for non-GET operations