How to make an HTTP call


#1

I have been following this documentation https://angular.io/docs/js/latest/api/http/Http-class.html#!#stq=http&stp=1 but am unable to make it work.

I have a application server at http://localhost/testcall which returns a JSON such as this:

{ ‘name’: ‘bob’}

I would like to be able to make a POST call to the server and print this name to the console. May someone please show with a simple example how to make an HTTP request and print it to console (console being on the client side when the response comes back)?

Cheers!


#2

Try this one

      $http({
          url:'http://www.server.com/API/v1/login',
          method: 'POST',
          data: JSON.stringify({username:$scope.loginData.username,password:$scope.loginData.password}),
          headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
          }).then(function(response){
            $scope.response = response;
        });

#3
http
    .post(url, body, options)
    .subscribe( 
        response => console.log(response), 
        err => console.log(error), 
        () => console.log('complete')
    );