3

I need a second instance of jQuery, or at least of its AJAX facility. The problem is that I need to contact two servers, and they use different authentication methods.

So in one case I need to set a header:

$.ajaxSetup({
    headers: {
        'Authorization': auth_string
    }
});

And not in the other case. If I send headers, the request will fail.

As I need to get data from both servers all the time, I don't want to manipulate the headers before each AJAX call. So it would be nice to use two different AJAX/jQuery objects.

How can I achieve this?

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
helle
  • 11,183
  • 9
  • 56
  • 83
  • 1
    I wonder what would happen if you did `var jq2 = jQuery.extend(true, {}, jQuery);`, as inspired by [this answer](http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-an-object) – Mitya Apr 26 '16 at 15:14
  • Why dont you just use the url parameter of the ajax call? – Nadir Apr 26 '16 at 15:19
  • @Utkanos: If I try to use it it says: `it is not an object`. @Nadir: What do you mean? – helle Apr 26 '16 at 15:21
  • I'll recommend you remove the global config and instanciate two ajax options objects and use them on demand while doing the requests. It will be much simplier. – Mistic Apr 26 '16 at 15:27
  • @mistic can you post an example as answer – helle Apr 26 '16 at 15:29
  • `$.ajax({url: 'url that needs auth string', headers: { 'Authori...': whatever }}).then(function(result) { //your handler });`. For the other url, you dont have to add the haeder – Nadir Apr 26 '16 at 15:29

2 Answers2

3

You should just be able to make two separate settings objects, like so:

var serverSettings1 = { headers: { 'Authorization': auth_string1 } }

var serverSettings2 = { \\ no headers here }

And then pass them in when you make the ajax call:
$.ajax(serverSettings1); or $.ajax(url, serverSettings2);
The settings objects can also hold the type of request, url, etc.

(From http://api.jquery.com/jquery.ajax/)

Kogicon
  • 76
  • 3
1

Following my comment : I suggest not using the global config and use two ajax options object used on demand.

With some factorization it could be :

var optionsServerA = {
  headers: {
    'Authorization': auth_string
  }
};

var optionsServerB = {};


// request on server A
$.ajax(
  $.extend({
    url: 'http://servera',
    data: {}
  }, optionsServerA)
)
  .then(...);

// request on server B
$.ajax(
  $.extend({
    url: 'http://serverb',
    data: {}
  }, optionsServerB)
)
  .then(...);

And you can create wrappers arround "ajax" for better readability

Mistic
  • 1,377
  • 2
  • 13
  • 27
  • I am now using a similar solution. More flexible, I would say. I have an object, which handles my ajax calls, and this has a property `headers` which can be set similar to my previous function. and when I do the ajax call I use this: `$.ajax({url:...,data:...,headers:self.headers,...})` while self (`var self = this;`) holds the object in my method. still ... I am waiting for an answer how to create a second instance for some days. – helle Apr 26 '16 at 18:37