python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions