Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of jQuery ajax beforeSend in Angularjs?

I am familiar with Jquery AJAX call, which has different callbacks like beforeSend, success, complete, etc.

This is the example AJAX call with Jquery:

$.ajax({
  url: 'register.php',
  type: 'POST',
  data: {name:name, email:email},
  beforeSend: function() {
       // show loading GIF
  },
  complete: function() {
      // hide loading GIF
  },
  success: function(data) {
      // parse response
  }
});

I want to achieve the same using AngularJS.

Is there a callback like beforeSend for AngularJS AJAX request ? This is my code so far, but i am not sure where can i use a callback like beforeSend (so that i can display a loading GIF image) in my code:

$http.post('register.php', {'name': $scope.name, 'email': $scope.email})
.success(function(data, status, headers, config) {
    if (data != '') { 
    }
});
like image 907
shasi kanth Avatar asked Mar 03 '14 07:03

shasi kanth


People also ask

What is Ajax in AngularJS?

The AngularJS provides a control service named as AJAX – $http, which serves the task for reading all the data that is available on the remote servers. The demand for the requirement of desired records gets met when the server makes the database call by using the browser. The data is mostly needed in JSON format.

Is Ajax and AngularJS same?

Angular vs AJAXAngular is more general whereas AJAX is more specific. In other words, Angular is a big framework whereas AJAX is a JavaScript method that enables asynchronous communication between the database and the server. Angular uses AJAX technology to build single-page applications.

Does jQuery contain Ajax?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!


2 Answers

By default Angular does not provide beforeSend and complete but you can implement them by using interceptors. Here is my implementation:

(function() {
    var app = angular.module("app");

    app.factory("interceptors", [function() {

        return {

            // if beforeSend is defined call it
            'request': function(request) {

                if (request.beforeSend)
                    request.beforeSend();

                return request;
            },


            // if complete is defined call it
            'response': function(response) {

                if (response.config.complete)
                    response.config.complete(response);

                return response;
            }
        };

    }]);

})();

Then you have to register your interceptor like this:

    (function () {
        var app = angular.module('app', ['ngRoute']);


        app.config(["$routeProvider", "$httpProvider", function ($router, $httpProvider) {    

            // Register interceptors service
            $httpProvider.interceptors.push('interceptors');

            $router.when("/", { templateUrl: "views/index.html" })


            .otherwise({ redirectTo: "/" });        
        }]);
    })();

After this code you can use it like this:

var promise = $http({
    method: 'POST',
    url: constants.server + '/emails/send',
    data: model,
    beforeSend: function() {
        model.waiting = true;
    },
    complete: function() {
        model.waiting = false;
    }
});
like image 92
Ro. Avatar answered Oct 27 '22 00:10

Ro.


You can use interceptors. Search for the word interceptor into the Angular $http documentation

As the documentation says : For purposes of global error handling, >authentication, or any kind of synchronous or asynchronous pre-processing of >request or postprocessing of responses

Here is a good Fiddle Example displaying a loading gif before the ajax call is sent.

EDIT

As Satpal commented, $httpProvider.responseInterceptors used in the Fiddle is deprecated. You should use $httpProvider.interceptors instead.

like image 29
Merlin Avatar answered Oct 26 '22 23:10

Merlin