JSFiddle - React, Tailwind, and code Playground

by Daniel Lamb

HTML

<div ng-app="myApp">
    <div ng-controller="Example">
        <button ng-click="post()">Post as form data</button>
    </div>
</div>

JavaScript

angular.module('myApp', [])
.controller('Example', ['$http', '$scope', function ($http, $scope) {
    $scope.post = function(e) {
        $http.post('/echo/json/',
          // data to post to the server
          { foo: 'bar' },
          {
            // serialize the JSON data
            transformRequest: function(json) {
              var data = [];
              angular.forEach(json, function(value, key){
                  data.push(encodeURIComponent(key) + '=' + 
                            encodeURIComponent(value));
              });              
              return data.join('&');
            },
            // add custom header
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
          }
        )
        .success(function(data, status, headers, config) {
          console.log('success', data);
        });
    };
}]);