How to use the $http.get() and $http.post() services in angularjs?

How to use the $http.get() and $http.post() services in angularjs?

In this topic I am going to share the use the GET method and POST services method in AngularJs. So look at given below code-sample.

The Table of Contents

1. The $http.get() Services 
2. The $http.post() Services 

The $http.get() Services

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
var app = angular.module('userApp', []);
app.service('userService', function ($http) {
    delete $http.defaults.headers.common['X-Requested-With'];
    this.getUsers = function () {
        // $http() GETmethod returns a $promise that we can add handlers with .then() function.
        return $http({
            method: 'GET',
            url: 'http://localhost:56110/APIs/Setting/Get'          
        });
    }
});
app.controller('GetUserCtrl', function ($scope, userService) {
    $scope.users = null;
    userService.getUsers().then(function (respResult) {
        $scope.users = respResult;
    });
});
var app = angular.module('userApp', []);

app.service('userService', function ($http) {
    delete $http.defaults.headers.common['X-Requested-With'];
    this.getUsers = function () {
        // $http() GETmethod returns a $promise that we can add handlers with .then() function.
        return $http({
            method: 'GET',
            url: 'http://localhost:56110/APIs/Setting/Get'           
        });
    }
});

app.controller('GetUserCtrl', function ($scope, userService) {
    $scope.users = null;
    userService.getUsers().then(function (respResult) {
        $scope.users = respResult;
    });
});

The $http.post() Services

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var app = angular.module('userApp', []);
app.service('userService', function ($http) {
    delete $http.defaults.headers.common['X-Requested-With'];
    var inputData = {Name:'Anil Singh'};
    this.getUsers = function () {
        // $http() POST method returns a $promise that we can add handlers with .then() function.
        return $http({
            method: 'POST',
            url: 'http://localhost:56110/APIs/Setting/AddUser',
            params: inputData
        });
    }
});
app.controller('AddUserCtrl', function ($scope, userService) {
    $scope.result = null;
    userService.getUsers().then(function (resp) {
        $scope.result = resp;
        if(resp.status ===200){
            //TODO: your custom logic
        }
    });
});
var app = angular.module('userApp', []);

app.service('userService', function ($http) {
    delete $http.defaults.headers.common['X-Requested-With'];
    var inputData = {Name:'Anil Singh'};
    this.getUsers = function () {
        // $http() POST method returns a $promise that we can add handlers with .then() function.
        return $http({
            method: 'POST',
            url: 'http://localhost:56110/APIs/Setting/AddUser',
            params: inputData
        });
    }
});

app.controller('AddUserCtrl', function ($scope, userService) {
    $scope.result = null;
    userService.getUsers().then(function (resp) {
        $scope.result = resp;
        if(resp.status ===200){
            //TODO: your custom logic
        }
    });
});
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^