Categories: AngularJs

AJAX Http – AngularJS

$http service is used to get data from remote server. It makes HTTP request to get data.

Following are methods available in $http service:

.get()
.put()
.post()
.delete()
.head()
.jsonp()
.patch()

These are called short cut methods. See bellow example using method .get()

[html]

{{response}}

Myapp.controller('serviceCtrl', function($scope,$http) { $http.get("tryit/angularjs/hello.html").then(function (responseData) { $scope.response = responseData.data; }); });
[/html]

In expandable format .get() can be written as given example:

[html]

{{response}}

Myapp.controller('serviceCtrl', function($scope,$http) { $http.get({ method: "GET", url: "tryit/angularjs/hello.html" }).then(function successRes(responseData) { $scope.response = responseData.data; }, function errorsRes(responseData) { $scope.response = responseData.data; } ); });
[/html]

In above example example, when defining two functions, first function is called if request was success and second function is called in case of error in request or to handle exception/error.

Note: Response comes from serve as an object. This object has given properties:

.status: This is a numeric value that defines the HTTP status.
.statusText: This is a string that defines the HTTP status.
.config: This is an object that is used to initiate the request.
.data : This can be a string or array returned from server.
.headers: .header is used to get header information.

jeff77

Disqus Comments Loading...

Recent Posts

Noun

A Noun is the name of a person, place or thing. or Name of anything is noun. For example: Sita…

10 years ago

Introduction – AngularJS

Misko Hevery, a Google employee, developed AngularJS. First version 1.0 of AngularJS was released in 2012. Now AngularJS is fully…

9 years ago

Expressions – AngularJS

AngularJS expressions contain operators,literals, and variables.  AngularJS expressions can be written inside HTML while JavaScript expressions can't be.  AngularJS expressions…

9 years ago

Noun – Exercise

Some sentences are given below. Choose the nouns and mention the type of noun whether they are proper noun, common…

9 years ago

Modules – AngularJS

Modules are containers for appication controller. They contain different parts of an application. [html] {{ state + " " +…

9 years ago

Directives – AngularJS

Using Directives you can extend HTML attributes. In AngularJS you can define your own directives while it has its own…

9 years ago