AngularJS $http Service: Part 6

Before reading this article I recommend you visit my previous articles also.

    AngularJS $http Service

    AngularJS provides various types of services for various purposes like DOM manipulation functions, logging and services that help us to run functions asynchronously and so on.

    In addition, AngularJS also offers a core service for reading data from remote HTTP servers, the $http service that helps communicate with a remote HTTP server via browser requests that includes an XMLHttpRequest object or via JSONP. This section explains the $http service.


    To generate a HttpRequest, a configuration object is ed as an arrgument to the $http service function. It will return a promise as success and error.

    Get Request Example:
    1. $http.get('/url').  
    2.  success(function(data,status,headers,config){  
    3.  //  
    4.  }).  
    1. error(function(data,status,headers,config){  
    2. //when response with an error  
    3. })  
    POST Request Example:
    1. $'/url', {msg:'some msg !'}).  
    2.         success(function(data, status, headers, config) {  
    3.                 // when the response is available  
    4.         }).  
    1. error(function(data, status, headers, config) {         
    2. // or server returns response with an error status.  
    3. });  
    Shortcut Methods

    We have various shrotcut methods in AngularJS to write unit tests. It is necessary to URLs and the requested data is ed in a POST request.

    • $http.get
    • $http.head
    • $
    • $http.put
    • $http.delete
    • $http.jsonp
    • $http.patch

    get( url , [config] );

    We can do GET requests using a get() shortcut method by ing two parameters, the first one is the URL that specifies the destination of the request. The URL is of string type, another parameter is config that is an optional configuration object, here config is of object type.

    get( url , [config] ) will return a future object that is HttpPromise.

    head ( url , [config] );

    It is the shortcut method to do a head request. This method also has two parameters, namely the URL of the string type and a config of an object type. It returns a HttpPromise.

    post ( url , data, [config] );

    To do a POST request, a post shortcut method is used. We do have three parameters including an optional config, namely url, data and the optional config. url and config are the same as in methods already discussed. Here the data is the content that is being requested. This also returns a HttpPromise as the response.

    delete ( url , [config] );

    This method does the DELETE requet by ing an URL and an optional config object. It returns a HttpPromise.

    jsonp ( url, [config]);

    To do a jsonp request, this method is used. url here specifies the destination of the request. Returns a HttpRequet.

    patch ( url , data, [config]);

    What it does

    • Does a PATCH request.
    • url (specifying destination)
    • data (requested content)
    • config (configuration object)
    • HttpPromise
    The Angularjs documentation lists other services also. Here are some of them with details.                        



     Services Description



    We have gone through the basics of services, particularly the $http angularjs service. We have also exlained some of the shortcut methods. In a future article we will learn about practicle approaches of these services, we will learn how these services are used to fetch the data saved in the server and used to fetch the data from database. I hope you enjoyed, please do drop a response or share your knowledge over here.

    Happy Coding !

    Up Next
      Ebook Download
      View all
      View all