Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 we have in past examples, with a single call attribute containing the call sign of the station we're interested in. By passing this value as the data attribute in our argument to $http...