Mastering Web Application

(Rick Simeone) #1

Communicating with


a Back-end Server


More often than not web applications need to communicate with a persistent store to
fetch and manipulate data. This is especially true for CRUD-like applications where
data editing is the essential part.


AngularJS is well equipped to communicate with various back-ends using
XMLHttpRequest (XHR) and JSONP requests. It has a general purpose $http
service for issuing XHR and JSONP calls as well as a specialized $resource service
to easily target RESTful endpoints.


In this chapter, we are going to examine different APIs and techniques to
communicate with various back-ends. In particular, we are going to learn how to:



  • Make basic XHR calls using the $http service and test code relying on $http

  • Effectively work with asynchronous requests, using the promise API exposed
    by AngularJS in the $q service

  • Easily talk to RESTful endpoints using the dedicated $resource factory

  • Build a custom, $resource-like API tailored to our back-end needs


Making XHR and JSONP requests with $http


The $http service is a fundamental, all-purpose API for making XHR and JSONP calls.
The API itself is well-crafted and easy to use, as we are going to see shortly. Before
diving into the details of the $http API though, we need to learn a bit more about data
model of our sample SCRUM application so we can follow meaningful examples.

Free download pdf