Sunday, 29 May 2016

18 Anjularjs $http service

Anjularjs provides many built in services like $http, $log…ect, as well we can create write our own service, we generally called as “custom services”.

anjularjs $http service:

1     1.   It is used to make HTTP requests to remote server.
       2.  It is a function that has a single input parameter i.e configuration object.




       this request issues a GET request to specified url (http://localhost:4968/api/values/--service)in the configuration object. configuration object has many properties to prepare config object. such as method,url,params,data,headers,transformRequest, paramSerializer…ect.


swe written service request as configuration object in side $http.({ configuration object }) 
   configuration object is a javascript object in  $http service it has built feature to convert as json object.
 we can write service request as inline request without configuration object but has one restriction.

aWe can write same http request as below : inline requst
                 $http.get('http://localhost:8181/api/values/').then(function (response)
                   {
                                         // Sucess
                                          $scope.message = response.data[0];
                                          $scope.message += response.data[1];
                                      },function (reason)
                                      {
                                          // Failure
                                          $scope.message="error occured"

                                      });
   $scope.employees = $http.get('http://localhost:8181/api/values/');--- not valid

a small restriction here is $http.get, $http.post ..such inline request returns a $promise object so that we should use then function for inline requests.

Anjularjs provides another service is $log which help us to log in console window about request and respone information.




s$http service provides below outof box transfermations are :


No comments:

Post a Comment