O'Reilly logo

Data Visualization with D3 and AngularJS by Christoph Körner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

$http – the Angular wrapper for XHR

If we are developing a component for AngularJS, then we should use all the benefits and advantages that this framework provides, such as Promises, caching, mocking, and so on. For XHR, AngularJS provides an easy-to-use function that implements Promises. Let's take a look at an example:

var url = "files/access.log";
$http.get(url)
.then(function(response){
  console.log(response.data);
});

Looks pretty neat, doesn't it? This is exactly why we want to use an abstraction provided by AngularJS. Now, we can make an HTTP request with all the advantages from the AngularJS world. For completeness, let's also look at the POST request:

var url = "files/access.log"; var data = {'test': 'my-data'}; $http.post(url, data) .then(function(response){ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required