Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Angularjs http service - Example

DZone's Guide to

Using Angularjs http service - Example

· Integration Zone ·
Free Resource

Are your API program basics covered? Read the 5 Pillars of Full Lifecycle API Management eBook

Here I will show a sample angularJs http get request.

$scope.loadCustomerDetails = function(customerId) {

		var responsePromise = $http.get('/customerDetails.htm',{
			params : {
				data : customerId
			}
		});

		responsePromise.success(function(data, status, headers, config) {
			
			$scope.custDetails = data;
			
		});
		responsePromise.error(function(data, status, headers, config) {
			alert("Request failed!");
		});

	};


loadCustomerDetails is an angularjs function which takes one argument - customerId. Within this function, we make an ajax request to a REST endpoint 'customerDetails.htm' . This endpoint return data in json format. We are using promise to catch the result. Promise is an interface for interacting with an object that represents the result of an action that is performed asynchronously, and may or may not be finished at any given point in time.

If the request is a success, then the control goes to the responsepromise.success() function else to the error function.

Hope you got an into to the angularjs http service.

Establish API creation, publishing and discovery as a master practice with the API Management Playbook.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}