AngularJS $httpBackend

by navyflower

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-mocks.js"></script>
<div ng-app='mydevapp' ng-controller='Ctrl'>
  <button ng-click='succeedGET()'>try to succeed with GET</button>
  <button ng-click='failGET()'>try to fail with GET</button>
  <button ng-click='succeedPOST()'>try to succeed with POST</button>
  <button ng-click='failPOST()'>try to fail with POST</button>
  <p>{{info}}</p>
  <p>response:</p>
  <pre>
  {{response | json}}
  </pre>

</div>

JavaScript

angular.module('myapp', [])
  .controller('Ctrl', ['$scope', '$http', function($scope, $http) {
    $scope.succeedGET = function() {
      $http({
        method: 'GET',
        url: 'api/myGetSuccessUrl'
      }).then(function(response) {
          $scope.response = response;
          $scope.info = "$http GET **success** callback!";
        },
        function(response) {
          $scope.response = response;
          $scope.info = "$http GET **error** callback!"
        });
    };
    $scope.failGET = function() {
      $http({
        method: 'GET',
        url: 'api/myGetFailureUrl'
      }).then(function(response) {
          $scope.response = response;
          $scope.info = "$http GET **success** callback!";
        },
        function(response) {
          $scope.response = response;
          $scope.info = "$http GET **error** callback!"
        });
    };
    $scope.succeedPOST = function() {
      $http({
        method: 'POST',
        url: 'api/myPostSuccessUrl',
        mydata: {
          first: 'first',
          second: 'second'
        }
      }).then(function(response) {
          $scope.response = response;
          $scope.info = "$http POST **success** callback!";
        },
        function(response) {
          $scope.response = response;
          $scope.info = "$http POST **error** callback!"
        });
    };
    $scope.failPOST = function() {
      $http({
        method: 'POST',
        url: 'api/myPostFailureUrl',
        mydata: {
          first: 'first',
          second: 'second'
        }
      }).then(function(response) {
          $scope.response = response;
          $scope.info = "$http POST **success** callback!";
        },
        function(response) {
          $scope.response = response;
          $scope.info = "$http POST **error** callback!"
        });
    };


 ...