AngularJS | AJAX - $http
function studentController($scope,$https:) { var url = "data.txt"; $https:.get(url).success( function(response) { $scope.students = response; }); }
- .post()
- .get()
- .head()
- .jsonp()
- .patch()
- .delete()
- .put()
Method: There are lots of methods that can be used to call $http service, this are also shortcut methods to call $http service.
- .headers : To get the header information (A Function).
- .statusText: To define the HTTP status (A String).
- .status: To define the HTTP status (A Number).
- .data: To carry the response from the server (A string/ An Object).
- .config: To generate the request (An Object).
Properties: With the help of these properties, the response from the server is an object.
Example: First of all, we will have a file which is going to contain our data. For this example, we have the file data.txt, which will include the records of the student. An ajax call will be made by the $http service. It is going to divert & set the response to the students having priority. After this extraction, the tables will be drawn up in the HTML, which will be based on the students model.
[ { "Name" : "Ronaldo", "Goals" : 128, "Ratio" : "69%" }, { "Name" : "James", "Goals" : 007, "Ratio" : "70%" }, { "Name" : "Ali", "Goals" : 786, "Ratio" : "99%" }, { "Name" : "Lionel ", "Goals" : 210, "Ratio" : "100%" } ]
<!DOCTYPE html>
<html>
<head>
<title>AngularJS AJAX - $http</title>
<style>
table, th, td {
border: 1px #2E0854;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #F6ADCD;
}
table tr:nth-child(even) {
background-color: #42C0FB;
}
</style>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js">
</script>
</head>
<body>
<center>
<h1 style="color:green">GeeksforGeeks</h1>
<h3>AJAX - $http</h>
<div ng-app="" ng-controller="studentController">
<table>
<tr>
<th>Name</th>
<th>Goals</th>
<th>Ratio</th>
</tr>
<tr ng-repeat="student in students">
<td>{{ Player.Name }}</td>
<td>{{ Player.Goals}}</td>
<td>{{ Player.Ratio}}</td>
</tr>
</table>
</div>
<script>
function studentController($scope, $http) {
var url = "/data.txt";
$http.get(url).then( function(response) {
$scope.students = response.data;
});
}
</script>
</center>
</body>
</html>
