23
Connect AngularJS to Servers Examples on GitHub

How to connect AngularJS to servers

Embed Size (px)

DESCRIPTION

In this short tutorial, we are going to see how AngularJS communicates with various back-ends using a general purpose $http service for issuing XMLHttpRequest (XHR) and JSONP calls, as well as the $resource service to easily target RESTful endpoints. All examples code is available on GitHub: https://github.com/carlos-/ajs-connectserver

Citation preview

Page 1: How to connect AngularJS to servers

Connect AngularJS to ServersExamples on GitHub

Page 2: How to connect AngularJS to servers

- Making XHR requests

- Accessing to REST API

- Overcoming to CORS

agenda

Page 3: How to connect AngularJS to servers

MAKING XHR REQUESTS

Page 4: How to connect AngularJS to servers

Core Angular service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP.

$http

Success asynchr

Page 5: How to connect AngularJS to servers

l Add some authorization headersl Setting cachel Transforming the request or response

Configuring request

Page 6: How to connect AngularJS to servers

Dedicated methodsOne method for each type of XHRequest

Page 7: How to connect AngularJS to servers

Request data from a specified resource

GET vs POST methods

Submit data to be processed to a specified resource

Page 8: How to connect AngularJS to servers

Example POSTMAN

$http

Page 9: How to connect AngularJS to servers

Chained promisesUsing promises $http calls can be chained

Only if resolve

Any promise error

Page 10: How to connect AngularJS to servers

ACCESS TO REST API

Page 11: How to connect AngularJS to servers

$resource serviceA 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.

Page 12: How to connect AngularJS to servers

Declaration in factoryCalling the injected $resource function

Careful with ':'

Custom methods

Page 13: How to connect AngularJS to servers

Benefits of ngResource- Simplified code (encapsulation)- No callbacks (unless wanted)- Unit tests with ngResource- Custom methods

Page 14: How to connect AngularJS to servers

Example $resource

Page 15: How to connect AngularJS to servers

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 Restangularhttps://github.com/mgonto/restangular

More to come in Angular 2.0

Page 16: How to connect AngularJS to servers

OVERCOMING CORS

Page 17: How to connect AngularJS to servers

Overcoming same-origin policyCross-origin Resource Sharing (CORS) allows to get a resource from another domain, forbidden by browsers.

- External resources- Different domain or port

Solutions1. Modify server2. JSONP

Page 18: How to connect AngularJS to servers

Allow all originModify server so it can be access by all origins.

Page 19: How to connect AngularJS to servers

Example Modify Server

Access-Control-Allow-Origin: *

Page 20: How to connect AngularJS to servers

JSONPJSONP = 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>

Page 21: How to connect AngularJS to servers

Example JSONP

Page 22: How to connect AngularJS to servers

JSONP limitations- Only GET HTTP Request- Error handling is problematic- Security threads- Modify my NodeJS to accept JSONP

Page 23: How to connect AngularJS to servers

MANY THANKShttp://about.me/Carlos_Morales

https://github.com/carlos-/ajs-connectserver