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

Sending JSON to your web server using AngularJS

Sending JSON with AngularJS is as easy as providing a data attribute in the argument to your $http() method call. AngularJS will even encode the object as JSON on your behalf.

How to do it...

Like before, we'll make an AJAX request. This time, we include a data attribute:

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 request = { 
      call: "kf6gpe-7"
    };
    var promise = $http({
      url: "/", 
      method: "POST",
      data: request
    });
  };
}]);

How it works…

We define the JavaScript object request as ...

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