AngularJS has provided a $http service that works as an ajax call in jquery. It performs a function of reading data from the server. $http makes a call to the database and retrieves the desired data or records.
How to use?
1. you need to inject $http into your controller.
2. you need to fire a method. There are several shortcut methods of the $http service:-
• .delete()
• .get()
• .head()
• .jsonp()
• .patch()
• .post()
• .put()
To get the data you need to use .get() method. For example-
mainApp.controller( "editprofileController", function( $scope, $http, $state ) {
editProfileData['responsetype'] = 'json';
console.log( JSON.stringify( editProfileData ) );
var responsePromise = $http.get( BASE_URL+"editprofile",JSON.stringify( editProfileData ) );
responsePromise.success( function( data, status, headers, config ) {
console.log( JSON.stringify( data ) );
$scope.displaymsg = data.message;
});
responsePromise.error( function ( data, status, headers, config ) {
console.log( JSON.stringify( data ) );
} );
HTML-
<div ng-app="mainApp" ng-controller="editprofileController">
<p>Today's welcome message is:</p>
<h1>{{displaymsg}}</h1>
</div>
$http comes with two functions,i.e. what to do on success and what to do on failure or error condition.
Note:- This is just an example. A simpler syntax is-
var url = "data.txt";
$http.get(url).success( function(response) {
......
});
0 Comment(s)