ghanshyam.mirani
ghanshyam.mirani

Reputation: 3101

Handle ajax call using angularjs

I want to perform additional operation before calling http request(Ajax GET and POST)

suppose i have following code:

function CallHTTP()
{

    $.ajax({
        url: '/abcd/abcd',
        type: 'POST',
        success: function () {
            alert("Success");
        },
        error: function (arg1, arg2, arg3) {
            alert("error");
        }

    });

}

$(document).ready(function () {
    CallHTTP();
});

i want to handle above request using angularjs interceptor. is it possible ?

I tried following:

 angular.module('app', [])
          .config(['$httpProvider', function ($httpProvider) {
              $httpProvider.interceptors.push(['$location', '$q', function ($location, $q) {
                  return {
                      'request': function (request) {
                          console.log("abcd");
                          return request;
                      },
                      'responseError': function (response) {
                          if (response.status === 401) {
                              console.log("abcd2");
                              // do stuff
                          }
                          console.log("abcd1");
                          // otherwise, default behaviour
                          return $q.reject(response);
                      }
                  };
              }]);
          }]);

but above code doesn't execute when ajax call happens. how to achieve it ?

Thanks.

Upvotes: 0

Views: 140

Answers (1)

dev8080
dev8080

Reputation: 4020

You are using jquery ajax call and your settings are in the angular interceptor. Use $http to make use of your angular settings.

Upvotes: 2

Related Questions