JavaScript - 无法提交用户名,密码

时间:2016-06-21 06:43:50

标签: javascript jquery apache tomcat authentication

使用JavaScript但使用常规网络浏览器无法提交用户名/密码,而在地址栏中使用直接网址则通过在弹出窗口中手动插入用户名密码来实现。

如何解决?

<script>
function make_base_auth(user,password) {
  var tok = user + ':' + password;
  var hash = btoa(tok);
  return "Basic " + hash;
} // ends

$(document).ready(function() {  
  $.ajax({
    type: "GET",
    url: "http://www.example.be/a/b/c?id=9990",
    dataType: 'xml',
    async: false,
    data: '',
    beforeSend: function(xhr)
    {
      xhr.setRequestHeader('Authorization', make_base_auth("user1", "1234"));
    },
    success: function (data, textStatus, jqXHR){
      console.log(data, textStatus, jqXHR);
    }
  });  
});
</script>

来自服务器的错误:

enter image description here

1 个答案:

答案 0 :(得分:0)

提供标题:标题中的授权,缓存控制:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://www.example.be/a/b/c?id=9990",
  "method": "GET",
  "headers": {
    "authorization": make_base_auth(user,password),
    "cache-control": "no-cache"
  }
}

function make_base_auth(user,password) {
  var tok = user + ':' + password;
  var hash = btoa(tok);
  return "Basic " + hash;
} // ends


$.ajax(settings).done(function (response) {
  console.log(response);
});