Calling Restful API in Angular JS
Here, we will call the RESTful API from angular js.
Suppose there is a RESTful API which URL is: http://localhost:8080/saveDetails
And I want to save employee information using angular js. We will call the above API using "POST" method.
There is a button in html page. When I will click this button it will save the employee information. Data is being sent in JSON format.
Here, I am using "$resource" service to call RESTful API. The $resource service is a factory which creates a resource object that lets you interact with RESTful server-side data sources as per the angular js documentation.
Note: It requires the ngResource module of angular js to be installed.
1. Testing.html
<!DOCTYPE html> <html ng-app='demoApp'> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="../angular/angular.js"></script> <script src="../angular-resource/angular-resource.js"></script> <title></title> </head> <body ng-controller='demoCtrl'> <input type="button" value="Submit" ng-click="saveDetails()" /> <script src="testing.js"></script> </body> </html>
2. testing.js
var demoApp = angular.module("demoApp", ['ngResource']); demoApp.factory('employee', function ($resource) { return $resource('/employee/:cmd', {}, { saveDetails: { method: 'POST', isArray: true, params: { cmd: 'saveDetails' } }, }); }); demoApp.controller("demoCtrl", ['$scope', 'employee', function ($scope, employee) { var config = { "empID": 1001, "firstName": "My Name", }; $scope.saveDetails = function () { employee.saveDetails(config, function (data) { // here data will come as JSON array in "data" object // it will be used to show input source fields document.write(JSON.stringify(data)); }, function (err) { console.log('saveDetails error:', err); }); } } ]);
Hope this is helpful example for you. Leave your comment and provide your suggestion if any.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments