In out previous Article we discussed about Routing in AngularJS. Here we go about importance of $http service in Angularjs. To know more about the complete Services and Directives in AngularJS, Please visit AngularJS Training in Chennai.

 $http is a core AngularJS service which is used to create communication with remote http servers via Browser’s inbuilt object XMLHttpRequest.

 

Syntax:

$http.get(‘/someUrl’, config).then(successCallback, errorCallback);

$http.post(‘/someUrl’, data, config).then(successCallback, errorCallback);

$http.put(‘/someUrl’, data, config).then(successCallback, errorCallback);

$http.delete(‘/someUrl’, config).then(successCallback, errorCallback);

Explanation:

Here the get and post are the method of passing data to the url.  There are few more methods are available as follows.

 

  1. get method is used for send small amount of data and we can send data parameter via params
  2. post method is used for send large amount of data and we can send form data via parameter and also data as a object.
  3. put method is used for update data and we can send form data via parameter and also data as a object.
  4. delete method is used for delte data and we can send data parameter via params

In the above Example the data represents form data and config represents configuration of request in object format.

The $http service returns a promising object which is it will return a object through Success Callback or Error Callback.

 Let’s assume the promising object return a object called response, then we can get the bellow properties.

response.data – it returns a string or object. It should be the result or outcome of the url which we sent via $http service. In other words, it will be your outcome of business logic which you created in the url.

response.status – {number} – HTTP status code of the response.

response.headers – it returns an object which contain header information.

response.config – it returns an object which contains complete configuration of the request.

response.statusText – {string} – HTTP status text of the response.

To Know more please visit, AngularJS Course Content.