$HTTP Service In Angular

Introduction

AngularJS $http service is the one of the service among the 30 services of AngularJS, in this blog we will demonstrate how to use $http service in AngularJS.

Getting Started

AngularJS $http service is used to call remote http services like ASP.NET Web Service or ASP.NET Web APIs. it takes single argument that is configuration object and this configuration object is used to generate request and response. The config can be global config or can be overridden at run time for particular call.

Syntax
  1. $http({    
  2.   method: 'menthod_name',    
  3.   url: 'service_url',  
  4.   data:'{}'    
  5.  }).then(function successCallback(response) {    
  6.   }, function errorCallback(response) {    
  7.   });   

You can see above syntax contains three sections that is request, response and error part. request section generates request object and Request object contains Method Name, URL, Data,Content Type,Content Length etc.

The response section is callback and will be called asynchronously when the response is available. The response object contains five properties that is listed bellow.

  1. data - Is a string data type and the response body transformed with the transform functions.
  2. status - Is a int data type and is HTTP status code of the response. Status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called. Also, status codes less than -1 are normalized to zero. -1 usually means the request was aborted, e.g. using a config.timeout.
  3. headers-Is a function which takes header name as parameter.
  4. config-The configuration object that was used to generate the request.
  5. statusText-HTTP status text of the response.

The last section is handling error, it is called asynchronously if an error occurs or server returns response with an error status.

Using shortcut method of $http service remote services can be call as well, $http service supports seven shortcut method that is listed below. All shortcut methods require passing in the URL, and request data must be passed in for POST/PUT requests. An optional config can be passed as the last argument.

  1. $http.get
  2. $http.head
  3. $http.post
  4. $http.put
  5. $http.delete
  6. $http.jsonp
  7. $http.patch
Syntax
  1. $http.get('/ServiceUrl', config).then(successCallback, errorCallback);    
  2.  $http.post('/ServiceUrl', data, config).then(successCallback, errorCallback);    
Example

This example demonstrates use of get method by using both normal method and shortcut method. It requests google page and getting error code in response. For more details go through the code example. 
  1. <HTML ng-app="myapp">    
  2.  <TITLE> AngularJS learning(http Service)</TITLE>    
  3.  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>    
  4.  <script>    
  5.  var myapp = angular.module('myapp', []);    
  6.  myapp.controller('myappcont', function($scope, $http) {    
  7.  $scope.pg="";    
  8.  $scope.btnclick=function(){    
  9.  $http({     
  10.   method: 'GET',     
  11.   url: "https://www.google.co.in/"    
  12.   }).then(function successCallback(response) {    
  13.  $scope.pg=response.data;     
  14.   alert(response.data);    
  15.   }, function errorCallback(response) {     
  16.   alert('Error Code:'+response.status+',Message:'+response.statusText);    
  17.   });    
  18.  }    
  19.  $scope.btnsclick=function(){    
  20.  $http.get("https://www.google.co.in/")    
  21.    .then(function(response) {    
  22.    alert(response.status);    
  23.      $scope.pg = response.data;    
  24.    }    
  25.       ,function errorCallback(response){    
  26.        alert('Error Code:'+response.status+',Message:'+response.statusText);    
  27.       });    
  28.       }    
  29.  });    
  30.  </script>    
  31.  <BODY ng-controller="myappcont">    
  32.  <p>Click to load(Full method):<button ng-click="btnclick()">Click</button>    
  33.  <p>Click to load(Shortcut method):<button ng-click="btnclick()">Click</button>    
  34.  <div ng-include="pg"></div>    
  35.  </p>    
  36.  </BODY>    
  37.  </HTML>    
Ebook Download
View all
Learn
View all