CodingCarlos / angular-demos

0 stars 2 forks source link

Ajax #6

Open CodingCarlos opened 7 years ago

CodingCarlos commented 7 years ago

AJAX (Asynchronous JavaScript And XML) is the method you can get and send data to a remote server, and make your angular app 100% dynamic.

This happen by using HTTP(s). HTTP is a comunication protocol. It implements some methods for the different actions you want to do:

$http

Angular has a service called $http. Full documentation.

To use it, is as easy as injecting $http in your service, controller or directive, and:

$http.get('api.whatever.com/thing')
    .then(function(data, status, headers, config) {
        // Success !!
    }, function(data, status, headers, config) {
        // Error !!
    );

Note: You can also use .post, .put, .delete, .jsonp, .head or .patch

There is another complexer but completer syntax:

$http({
    method: 'GET',
    url: '/someUrl'
}).then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
    }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });

(Full documentation about this way in https://docs.angularjs.org/api/ng/service/$http#usage)