0

I am creating an ajax call as follows:

beforeSend: function(xhr) { 
    xhr.setRequestHeader("Authorization", make_base_auth(username, password)); 
},

The resultant HTTP request header contains

Access-Control-Request-Headers: authorization

When I issue the same request from the browser directly the request header correctly contains

Authorization: Basic ZG<etc>

Looking at the JQuery routines, jquery-1.20.2, the setRequestHeader is correctly adding the header name and value pair to the cached list of headers.

The AJAX call throws an error as a result of the authorization failing. (ZGetcetc matches what I see in the successful manual attempt)

2
  • 2
    "The ajax call throws an error as a result of the authorization failing." - what error. Also, can you share the code of make_base_auth ? Commented Oct 24, 2013 at 13:19
  • 1
    There is no 1.20.2 version of jQuery...? Commented Oct 24, 2013 at 13:27

1 Answer 1

2

I am not sure if this is a viable option for you; upgrade your jQuery.

Newever jQuery has the ability to pass the basic auth username and password as part of the ajax call, e.g.

$.ajax
({
  type: "GET",
  url: "index.php",
  dataType: 'json',
  async: false,
  username: "YOUR-USERNAME-HERE",
  password: "YOUR-PASSWORD-HERE",
  data: '{ "DATA-HERE" }',
  success: function (data) {
     // handle success data
  }
});

Src: http://api.jquery.com/jQuery.ajax/


Another option:

beforeSend: function (xhr) {
    xhr.setRequestHeader ("Authorization", "Basic "+ Base64.encode(username +':'+ password));
},
Sign up to request clarification or add additional context in comments.

1 Comment

Will it be safe to send credentials like this?

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.