SlideShare a Scribd company logo
Connect 
AngularJS to 
Servers 
Examples on GitHub
agenda 
- Making XHR requests 
- Accessing to REST API 
- Overcoming to CORS
MAKING XHR REQUESTS
$http 
Core Angular service that facilitates communication 
with the remote HTTP servers via the browser's 
XMLHttpRequest object or via JSONP. 
Success asynchr
Configuring request 
l Add some authorization headers 
l Setting cache 
l Transforming the request or response
Dedicated methods 
One method for each type of XHRequest
GET vs POST methods 
Request data from a specified resource 
Submit data to be processed to a specified resource
Example 
POSTMAN 
$http
Chained promises 
Using promises $http calls can be chained 
Only if resolve 
Any promise error
ACCESS TO REST API
$resource service 
A factory which creates a resource object that lets you 
interact with RESTful server-side data sources. 
The returned resource object has action methods 
which provide high-level behaviors without the need to 
interact with the low level $http service.
Declaration in factory 
Calling the injected $resource function 
Careful with ':' 
Custom methods
Benefits of ngResource 
- Simplified code (encapsulation) 
- No callbacks (unless wanted) 
- Unit tests with ngResource 
- Custom methods
Example 
$resource
Problems of ngResource 
- Any custom behavior expect (big) extra effort 
- Once you get the data you can not do much, so you 
should deliver it in its final state 
- Does not return promises (directly) 
- Building custom URLs is not easy 
Check Restangular 
https://github.com/mgonto/restangular 
More to come in Angular 2.0
OVERCOMING CORS
Overcoming same-origin policy 
Cross-origin Resource Sharing (CORS) allows to get a 
resource from another domain, forbidden by browsers. 
- External resources 
- Different domain or port 
Solutions 
1. Modify server 
2. JSONP
Allow all origin 
Modify server so it can be access by all origins.
Example 
Modify Server 
Access-Control-Allow-Origin: *
JSONP 
JSONP = JSON with padding 
Request data from a server in a different domain, 
taking advantage of the fact that browsers do not 
enforce the same-origin policy on <script> tags. 
<script type=”text/javascript” 
src=”...../api/?callback=angular.callbacks._1”> 
</script>
Example 
JSONP
JSONP limitations 
- Only GET HTTP Request 
- Error handling is problematic 
- Security threads 
- Modify my NodeJS to accept JSONP
MANY 
THANKS 
http://about.me/Carlos_Morales 
https://github.com/carlos-/ajs-connectserver

More Related Content

What's hot (20)

PDF
Real World Fun with ActiveResource
Rob C
 
PDF
Using the new WordPress REST API
Caldera Labs
 
PDF
Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017
Codemotion
 
PDF
Building RESTful applications using Spring MVC
IndicThreads
 
PDF
Effective Web Application Development with Apache Sling
Robert Munteanu
 
PDF
Using Sakai with Multiple Locales
ballsy333
 
PDF
RESTful web apps with Apache Sling - 2013 version
Bertrand Delacretaz
 
PPTX
Designing REST services with Spring MVC
Serhii Kartashov
 
KEY
CakePHP REST Plugin
Kevin van Zonneveld
 
PDF
Consuming REST services with ActiveResource
Wolfram Arnold
 
PPTX
REST API for your WP7 App
Agnius Paradnikas
 
PDF
[JCConf 2020] 用 Kotlin 跨入 Serverless 世代
Shengyou Fan
 
PDF
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
Caldera Labs
 
PDF
Extending the WordPress REST API - Josh Pollock
Caldera Labs
 
PDF
Building an API with Django and Django REST Framework
Christopher Foresman
 
PDF
Retrofit
Amin Cheloh
 
PDF
CIRCUIT 2015 - 10 Things Apache Sling Can Do
ICF CIRCUIT
 
PPTX
Building multi lingual and empatic bots - Sander van den Hoven - Codemotion A...
Codemotion
 
PPTX
Node js crash course session 2
Abdul Rahman Masri Attal
 
PDF
API Days Australia - Automatic Testing of (RESTful) API Documentation
Rouven Weßling
 
Real World Fun with ActiveResource
Rob C
 
Using the new WordPress REST API
Caldera Labs
 
Going fullstack React(ive) - Paulo Lopes - Codemotion Amsterdam 2017
Codemotion
 
Building RESTful applications using Spring MVC
IndicThreads
 
Effective Web Application Development with Apache Sling
Robert Munteanu
 
Using Sakai with Multiple Locales
ballsy333
 
RESTful web apps with Apache Sling - 2013 version
Bertrand Delacretaz
 
Designing REST services with Spring MVC
Serhii Kartashov
 
CakePHP REST Plugin
Kevin van Zonneveld
 
Consuming REST services with ActiveResource
Wolfram Arnold
 
REST API for your WP7 App
Agnius Paradnikas
 
[JCConf 2020] 用 Kotlin 跨入 Serverless 世代
Shengyou Fan
 
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
Caldera Labs
 
Extending the WordPress REST API - Josh Pollock
Caldera Labs
 
Building an API with Django and Django REST Framework
Christopher Foresman
 
Retrofit
Amin Cheloh
 
CIRCUIT 2015 - 10 Things Apache Sling Can Do
ICF CIRCUIT
 
Building multi lingual and empatic bots - Sander van den Hoven - Codemotion A...
Codemotion
 
Node js crash course session 2
Abdul Rahman Masri Attal
 
API Days Australia - Automatic Testing of (RESTful) API Documentation
Rouven Weßling
 

Viewers also liked (7)

PDF
AngularJS application architecture
Gabriele Falace
 
PDF
Swagger con JBoss
Nunzio Mastrapasqua
 
PPTX
REST in short
Akshay Ballarpure
 
PDF
SOFEA: Service Oriented Front End Architecture, Next Gen Web Architecture for...
Kunal Ashar
 
PDF
TDC 2014 - Arquitetura front-end com AngularJS
Leonardo Zanivan
 
PDF
Angular 2 for Java Developers
Yakov Fain
 
PPTX
REST API Design
Devi Kiran G
 
AngularJS application architecture
Gabriele Falace
 
Swagger con JBoss
Nunzio Mastrapasqua
 
REST in short
Akshay Ballarpure
 
SOFEA: Service Oriented Front End Architecture, Next Gen Web Architecture for...
Kunal Ashar
 
TDC 2014 - Arquitetura front-end com AngularJS
Leonardo Zanivan
 
Angular 2 for Java Developers
Yakov Fain
 
REST API Design
Devi Kiran G
 
Ad

Similar to How to connect AngularJS to servers (20)

PDF
Build REST API clients for AngularJS
Almog Baku
 
ODP
Consume RESTful APIs with $resource and Restangular
John Schmidt
 
PDF
Learn Angular 9/8 In Easy Steps
Ahmed Bouchefra
 
PDF
FITC presents: Mobile & offline data synchronization in Angular JS
FITC
 
PDF
Advanced I/O in browser
Eugene Lazutkin
 
PPTX
The future of web development write once, run everywhere with angular.js and ...
Mark Roden
 
PDF
The future of web development write once, run everywhere with angular js an...
Mark Leusink
 
PPTX
REST Easy with AngularJS - ng-grid CRUD Example
Backand Cohen
 
PPTX
Angular js 1.0-fundamentals
Venkatesh Narayanan
 
PPTX
Single page application 05
Ismaeel Enjreny
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PDF
Laravel + Restangular Introduction
Andrew Del Prete
 
PPTX
Vancouver AngularJS using $resource in your application
Sachin Agrawal
 
PPTX
Agular in a microservices world
Brecht Billiet
 
PPTX
AngularJS 1.x - your first application (problems and solutions)
Igor Talevski
 
PDF
Using JHipster for generating Angular/Spring Boot apps
Yakov Fain
 
PPTX
Pentesting Modern Web Apps: A Primer
Brian Hysell
 
KEY
Javascript Frameworks for Well Architected, Immersive Web Apps
dnelson-cs
 
PDF
AngularJS Tips&Tricks
Petr Bela
 
Build REST API clients for AngularJS
Almog Baku
 
Consume RESTful APIs with $resource and Restangular
John Schmidt
 
Learn Angular 9/8 In Easy Steps
Ahmed Bouchefra
 
FITC presents: Mobile & offline data synchronization in Angular JS
FITC
 
Advanced I/O in browser
Eugene Lazutkin
 
The future of web development write once, run everywhere with angular.js and ...
Mark Roden
 
The future of web development write once, run everywhere with angular js an...
Mark Leusink
 
REST Easy with AngularJS - ng-grid CRUD Example
Backand Cohen
 
Angular js 1.0-fundamentals
Venkatesh Narayanan
 
Single page application 05
Ismaeel Enjreny
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Laravel + Restangular Introduction
Andrew Del Prete
 
Vancouver AngularJS using $resource in your application
Sachin Agrawal
 
Agular in a microservices world
Brecht Billiet
 
AngularJS 1.x - your first application (problems and solutions)
Igor Talevski
 
Using JHipster for generating Angular/Spring Boot apps
Yakov Fain
 
Pentesting Modern Web Apps: A Primer
Brian Hysell
 
Javascript Frameworks for Well Architected, Immersive Web Apps
dnelson-cs
 
AngularJS Tips&Tricks
Petr Bela
 
Ad

Recently uploaded (20)

PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Biography of Daniel Podor.pdf
Daniel Podor
 
July Patch Tuesday
Ivanti
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 

How to connect AngularJS to servers

  • 1. Connect AngularJS to Servers Examples on GitHub
  • 2. agenda - Making XHR requests - Accessing to REST API - Overcoming to CORS
  • 4. $http Core Angular service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP. Success asynchr
  • 5. Configuring request l Add some authorization headers l Setting cache l Transforming the request or response
  • 6. Dedicated methods One method for each type of XHRequest
  • 7. GET vs POST methods Request data from a specified resource Submit data to be processed to a specified resource
  • 9. Chained promises Using promises $http calls can be chained Only if resolve Any promise error
  • 11. $resource service A factory which creates a resource object that lets you interact with RESTful server-side data sources. The returned resource object has action methods which provide high-level behaviors without the need to interact with the low level $http service.
  • 12. Declaration in factory Calling the injected $resource function Careful with ':' Custom methods
  • 13. Benefits of ngResource - Simplified code (encapsulation) - No callbacks (unless wanted) - Unit tests with ngResource - Custom methods
  • 15. Problems of ngResource - Any custom behavior expect (big) extra effort - Once you get the data you can not do much, so you should deliver it in its final state - Does not return promises (directly) - Building custom URLs is not easy Check Restangular https://github.com/mgonto/restangular More to come in Angular 2.0
  • 17. Overcoming same-origin policy Cross-origin Resource Sharing (CORS) allows to get a resource from another domain, forbidden by browsers. - External resources - Different domain or port Solutions 1. Modify server 2. JSONP
  • 18. Allow all origin Modify server so it can be access by all origins.
  • 19. Example Modify Server Access-Control-Allow-Origin: *
  • 20. JSONP JSONP = JSON with padding Request data from a server in a different domain, taking advantage of the fact that browsers do not enforce the same-origin policy on <script> tags. <script type=”text/javascript” src=”...../api/?callback=angular.callbacks._1”> </script>
  • 22. JSONP limitations - Only GET HTTP Request - Error handling is problematic - Security threads - Modify my NodeJS to accept JSONP
  • 23. MANY THANKS http://about.me/Carlos_Morales https://github.com/carlos-/ajs-connectserver