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?

6
  • 1
    I wonder what would happen if you did var jq2 = jQuery.extend(true, {}, jQuery);, as inspired by this answer Commented Apr 26, 2016 at 15:14
  • Why dont you just use the url parameter of the ajax call? Commented Apr 26, 2016 at 15:19
  • @Utkanos: If I try to use it it says: it is not an object. @Nadir: What do you mean? Commented Apr 26, 2016 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. Commented Apr 26, 2016 at 15:27
  • @mistic can you post an example as answer Commented Apr 26, 2016 at 15:29

2 Answers 2

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/)

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.

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.