angular.js: CRUD operations via $http

These are the basic operations you can do with $http:

  • $http.get(): Accepts aURL and optional config object. Performs an HTTP GETrequest.
  • $http.head(): Accepts a URL and optional config object. Performs an HTTP HEADrequest.
  • $http.post(): Accepts a URL, data object, and optional config object. Performs an HTTP POST request.
  • $http.put(): Accepts a URL, data object, and optional config object. Performs an HTTP PUT request.
  • $http.delete(): Accepts a URL and optional config object. Performs an HTTPDELETE request.
  • $http.jsonp(): Accepts a URL and optional config object. The callback name should be the string JSON_CALLBACK.
  • $http.patch(): Accepts a URL, data object, and optional config object. Performs an HTTP PUT request.

Example of calling the get() method:

$http({
    method: 'GET',
    url: 'http://localhost:8000'
});

$http.get('http://localhost:8000');

You can easily chain promises to handle success or error returns from the server:

$http.get('http://localhost:8000')
.success(function(data){
    $scope.contacts = data;
})
.error(function(){
    window.alert('There was an error!');
});

Example of a post:

$http.post('http://localhost:8000', {
    name: 'Declan Proud',
    email: 'declan@example.com',
    ...
});

Leave a comment

Your email address will not be published. Required fields are marked *