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)

4

1 回答 1

2

我不确定这对您来说是否可行;升级你的 jQuery。

Newever jQuery 能够将基本身份验证用户名和密码作为 ajax 调用的一部分传递,例如

$.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
  }
});

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


另外的选择:

beforeSend: function (xhr) {
    xhr.setRequestHeader ("Authorization", "Basic "+ Base64.encode(username +':'+ password));
},
于 2013-10-24T13:25:43.117 回答