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:
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.