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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book