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

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

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.

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}