AngularJS – Ajax

The $http service helps to call the ajax request in angularjs. The $http service in angular js helps to perform http request. It also allows you create any http request by just injecting it via controller constructor.
It also helps to fetch the data from remote server. It needs the data available at the server must be in JSON format or an javascript object. $http fetches the data from the server using browser.

In AngularJS you can send AJAX requests in several different ways. These are:

  • AJAX calls via the $http service.
  • JSONP calls via the $http service.
  • REST type calls.




Syntax of $http:

	modulename.controller('controllername',function($http) {
		$http({
			method: 'GET',
			url: 'enter url here'
		}).then(function successCallback(response) {
		}, function errorCallback(response) {
		});
	});

Method: GET and POST methods.





Syntax

	method: 'GET',   

url: In here we define the url where we fetch the data. We get the data from this specified url.





Syntax

url: 'enter url here'

successCallback: This finction will get called when our server returns response.

Syntax

.then(function successCallback(response) {}




errorCallback: This function will get called when server returns error.

Syntax

 function errorCallback(response) {}

Leave a Reply

Your email address will not be published.