0

Very simple scenario:

Code:

$.ajax({
    type: "GET/POST",
    url: 'http://somewhere.net/',
    data: data,
    beforeSend: '', // custom property
}).done().fail();

Basically I am looking for a legit way to modify beforeSend within Angular factory service. Please refer to what I have:

myApp.factory('GetBalance', ['$resource', function ($resource) {
    return $resource('/Service/GetBalance', {}, {
        query: { method: 'GET', params: {}, }, isArray: true,
    });
}]);

Here I am using AngularJs services api problem is the documentation is not very helpful towards what I am trying to achieve.

3 Answers 3

2

If you want custom headers to be attached, you can do like this.

myApp.factory('GetBalance', ['$resource', function ($resource) {
    return $resource('/Service/GetBalance', {}, {
        query: { method: 'GET', params: {}, headers: { 'beforeSend': '' } }, isArray: true,
    });
}]);
Sign up to request clarification or add additional context in comments.

2 Comments

Apologies for not getting back sooner. I only got to test it properly just now. For whatever reason I can't seem to be able to find additional headers attached to the request when I view it in Chrome's Dev tools or Fiddler. Do I need to configure something like $httpProvider in module .config() ?
Its been a while since I looked back. But I wanted to go back and report on my findings. I found respected reference in Angular documentation but for what ever reason it just wouldn't work for me. So ended up using $httpProvider.defaults.headers.common[] array to define custom params in config function.
2

You might be interested in requestInterceptors, especially on the addFullRequestInterceptor method which has the following parameters:

It can return an object with any (or all) of following properties:

  • headers: The headers to send
  • params: The request parameters to send
  • element: The element to send
  • httpConfig: The httpConfig to call with

Comments

0

Solution sugested by @Thalaivar should have worked according to Angular docs but for whatever reason it just wouldn't work in my specific use case.

Here I provide an alternative solution that worked out for me as follows:

myApp.config(['$provide', '$httpProvider', function ($provide, $httpProvider) {
    // Setup request headers
    $httpProvider.defaults.headers.common['modId'] = 1;     // dynamic var
    $httpProvider.defaults.headers.common['pageId'] = 2;    // dynamic var
    $httpProvider.defaults.headers.common['token'] = 'xyz'; // dynamic var
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
});

I know that headed values are hard coded in my example but in reality things change from page to page so those values are actually dynamically retrieved.

Again if you are using $http based services then you should not need to do this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.