Ways to Communicate with REST Services

Categories: Angular Angular JS

Ways to Communicate with REST Services

 

There are currently two ways to communicate with REST services using AngularJS:

   The $http service

This service provides low-level interaction with REST services using the browser’s

   XMLHttpRequest object.

   The $resource object

This object provides a high-level approach to interacting with REST services, simplifying the process considerably.

We will focus mostly on using the $resource object for communicating with REST services and leave the $http service discussion to other books (although we will use the $http service in later chapters for handling Basic Authentication headers). All our project code uses the $resource object.

The following code shows how to define an AngularJS service that can be used to interact with the BlogPost REST service. Notice that we pass the REST service URL to the $resource object. The methods defined match the REST services that are defined on that particular URL. Once the BlogPost service is defined, it can be used like a standard JavaScript object to access the different REST services defined on this URL:

/* chapter6/services.js */

'use strict';

/* Services */

var blogServices =

angular.module('blogServices', ['ngResource']);

blogServices.factory('BlogPost', ['$resource',

function($resource) {

return $resource("http://www.micbutton.com/rs/blogPost", {}, {

get: {method: 'GET', cache: false, isArray: false},

save: {method: 'POST', cache: false, isArray: false},

update: {method: 'PUT', cache: false, isArray: false},

delete: {method: 'DELETE', cache: false, isArray: false}

});

}]);

Using the $resource object is by far the easiest way to call REST services. As you can see from this example, the AngularJS service code is straightforward and really fairly uncomplicated. Even when many services are defined, the services.js file is relatively simple.

The AngularJS $http service mentioned earlier is another way to call REST services. However, using the $http service would require many more lines of code related to REST service calls than we need using the $resource object. We do use the $http service in several places in the blog application, though, such as to send a Basic Authentication header to REST services.

Top Blogs
Angular and Node JS difference Published at:- The top 5 new features of Angular did you know Published at:- Introduction to AngularJS Published at:- Single-Page Applications and Bootstrapping the Application in Angular JS Published at:- Dependency Injection and AngularJS Routes Published at:- AngularJS Templates, Views, Models, Controllers Published at:- Integrating AngularJS with Other Frameworks Published at:- Testing AngularJS Applications in the IDE Published at:- End-to-End Testing with Protractor Published at:- AngularJS Views and Bootstrap Published at:- Adding a New Blog Controller Published at:- Adding a New Blog Template In Angular JS Published at:- Ways to Communicate with REST Services Published at:- Services and Business Logic and Handling User Authentication in Angular JS Published at:- Using Basic Authentication, Creating AngularJS Services, Holding User Credentials in Angular JS Published at:- AngularJS Security : why we are covering security in a book on AngularJS Published at:- MEAN Cloud and Mobile, Local Deployment and Installing Node.js, npm, and MongoDB in Angular Published at:- Angular 4 Questions - Angular 4 Quiz (MCQ) Published at:- AngularJS MCQ Quiz Questions with Answer Part 2 Published at:- AngularJS MCQ Quiz Questions with Answer Published at:- AngularJS MCQ Quiz Questions with Answer Published at:- Angular Interview Question Set 1 Published at:- Angular Interview Question Set 2 Published at:- Angular Interview Question Set 3 Published at:- Angular Interview Question Set 4 Published at:- Angular Interview Question Set 5 Published at:- Angular Interview Question Set 7 Published at:- Angular Interview Question Set 8 Published at:- Angular Interview Question Set 9 Published at:- Angular Interview Question Set 10 Published at:- Angular Interview Question Set 11 Published at:- Angular Interview Question Set 11 Published at:- Angular Interview Question Set 12 Published at:- Angular Interview Question Set 13 Published at:- Angular Interview Question Set 14 Published at:- Angular Interview Question Set 15 Published at:- React versus Angular What Would it be advisable for You Pick Published at:- 9 Advantages of Angular you really want to be aware if you have any desire to assemble Computerized Items Published at:- Compromises Between The Great And Terrible Sides Of Angular Development Published at:- Top 20 Angular 10 Inquiries Questions and Answer Published at:- 9 Advantages of Angular you want to be aware to assemble Advanced Items Published at:- Top 5 Elements Angular Favored Decision for Web Improvement Published at:- Exploring the Latest Version of AngularJS: What's New and Exciting Published at:-
R4R.co.in Team
The content on R4R is created by expert teams.