×

Html & Html5

The World's best Software Development Study Portal

AngularJS AJAX - $http


  • AngularJS provides a $http service for reading data and remote servers.

  • It is used to retrieve the desired records from a server. AngularJS requires data in JSON format.

  • The AngularJS provides a control service named as AJAX – $http, which serves the task for reading all the data that is available on the remote servers.

  • The demand for the requirement of desired records gets met when the server makes the database call by using the browser. The data is mostly needed in JSON format.

  • This is primarily because for transporting the data, JSON is an amazon method and also it is straightforward & effortless to use within AngularJS, JavaScript, etc.




  • Syntax:

    function studentController($scope,$https:) {
       var url = "data.txt";
    
       $https:.get(url).success( function(response) {
          $scope.students = response; 
       });
    }
    



    Make a simple request to the server, and display the result in a header:

    <div ng-app="myApp" ng-controller="myCtrl"> <h1>{{myWelcome}}</h1> </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $http) { $http.get("welcome.html") .then(function(response) { $scope.myWelcome = response.data; }); }); </script>
    Output
    Hello IT training institute



    Methods:


    There are lots of methods that can be used to call $http service, this are also shortcut methods to call $http service.


    1).post()

    2).get()

    3).head()

    4).jsonp()

    5).patch()

    6).delete()

    7).put()



    $http service makes a request to the server

    <div ng-app="myApp" ng-controller="myCtrl"> <p>Data : {{content}}</p> <p>Status : {{statuscode}}</p> <p>StatusText : {{statustext}}</p> </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $http) { $http.get("welcome.htm") .then(function(response) { $scope.content = response.data; $scope.statuscode = response.status; $scope.statustext = response.statusText; }); }); </script>
    Output

    Data : Hello AngularJS Students

    Status : 200

    StatusText : IT training institute




    Properties:


    With the help of these properties, the response from the server is an object.

    1).headers: To get the header information (A Function).

    2).statusText: To define the HTTP status (A String).

    3).status: To define the HTTP status (A Number).

    4).data: To carry the response from the server (A string/ An Object).

    5).config:To generate the request (An Object).




    Example
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $http) {
      $http.get("welcome.htm")
      .then(function(response) {
        $scope.content = response.data;
        $scope.statuscode = response.status;
        $scope.statustext = response.statusText;
      });
    });




    Let us see an another Example:To handle errors,add function .then method

    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope, $http) {
      $http.get("wrongfilename.htm")
      .then(function(response) {
        // First function handles success
        $scope.content = response.data;
      }, function(response) {
        // Second function handles error
        $scope.content = "Something went wrong";
      });
    });



    Note:

    The example above executes the $http service with an object as an argument. The object is specifying the HTTP method, the url, what to do on success, and what to do on failure.