The $http
service is a core Angular service. This service is used to communicate with remote HTTP servers via the browser's XMLHttpRequest
object or JSONP. The $http
API is based on the promise APIs exposed by the $q
service. This service, unsurprisingly, exposes methods that reflect the HTTP verb names, get
, head
, post
, put
, delete
, and so on. The typical way of calling these methods is as follows:
$http.get('/someUrl') .success(function(data, status, headers, config) { // this callback is called asynchronously // when the response is available }) .error(function (data, status, headers, config) { // called asynchronously if an error occurs // or server returns response with an error status });
So, let's see an example of using the $http
service. Postcodes.io is a free and open source postcode and Geolocation API for the UK. It exposes many API endpoints. We'll specifically use the one that gives us information about a random postcode...