O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

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

Requesting JSON content using AngularJS

Angular defines a core object, $http, which you use to make HTTP requests of remote servers. It's passed to your controller when you initialize it.

How to do it...

Let's extend our controller to add a reference to the $http object and use it to make a request:

var app = angular.module("aprsapp", []);
 
app.controller("AprsController", ["$scope", "$http",
function($scope, $http) {
  $scope.json = "";
  $scope.message = "Loaded..."; 
  $scope.doAjax = function()
  {
    $scope.debug = "Fetching...";    
    $scope.json= "";
    $scope.message = "";

    var promise = $http({
      url: "/", 
      method: "POST",
    });
  };
}]);

Here, we define a function doAjax in our scope that will perform the asynchronous HTTP request. It updates our models so that the ...

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