Angularjs

23
Bantayso solutions Connect AngularJS to Servers Examples on GitHub

description

Angular JS https://www.facebook.com/groups/359817260850658/

Transcript of Angularjs

Page 1: Angularjs

Bantayso solutions

Connect AngularJS to Servers

Examples on GitHub

Page 2: Angularjs

Bantayso solutions

Agenda

- Making XHR requests - Accessing to REST API - Overcoming to CORS

Page 3: Angularjs

Bantayso solutions

MAKING XHR REQUESTS

Page 4: Angularjs

Bantayso solutions

$http

Core Angular service that facilitates communication with the remote HTTP servers via the browser's

XMLHttpRequest object or via JSONP.

Success asynchr

Page 5: Angularjs

Bantayso solutions

Coniguring request

l Add some authorization headers

l Setting cache

l Transforming the request or response

Page 6: Angularjs

Bantayso solutions

Dedicated methods

One method for each type of XHRequest

Page 7: Angularjs

Bantayso solutions

GET vs POST methods

Request data from a speciied resource

Submit data to be processed to a speciied resource

Page 8: Angularjs

Bantayso solutions

Example

POSTMAN $http

Page 9: Angularjs

Bantayso solutions

Chained promises

Using promises $http calls can be chained

Only if resolve

Any promise error

Page 10: Angularjs

Bantayso solutions

ACCESS TO REST API

Page 11: Angularjs

Bantayso solutions

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

Page 12: Angularjs

Bantayso solutions

Declaration in factory

Calling the injected $resource function

Careful with ':'

Custom methods

Page 13: Angularjs

Bantayso solutions

Beneits of ngResource

- Simpliied code (encapsulation) - No callbacks (unless wanted) - Unit tests with ngResource - Custom methods

Page 14: Angularjs

Bantayso solutions

Example

$resource

Page 15: Angularjs

Bantayso solutions

Problems of ngResource

- Any custom behavior expect (big) extra efort - Once you get the data you can not do much, so you should deliver it in its inal 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

Page 16: Angularjs

Bantayso solutions

OVERCOMING CORS

Page 17: Angularjs

Bantayso solutions

Overcoming same-origin policy

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

- External resources - Diferent domain or port

Solutions

1. Modify server 2. JSONP

Page 18: Angularjs

Bantayso solutions

Allow all origin

Modify server so it can be access by all origins.

Page 19: Angularjs

Bantayso solutions

Example

Modify Server

Access-Control-Allow-Origin: *

Page 20: Angularjs

Bantayso solutions

JSONP

JSONP = JSON with padding

Request data from a server in a diferent 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: Angularjs

Bantayso solutions

Example

JSONP

Page 22: Angularjs

Bantayso solutions

JSONP limitations

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

Page 23: Angularjs

Bantayso solutions

MANY

THANKS

http://bantayso.com