簡體   English   中英

jQuery-Ajax beforeSend基本身份驗證未按預期工作

[英]Jquery - ajax beforeSend basic authentication not working as expected

我正在嘗試使用ajax將請求發送到受以下基本代碼保護的服務器:

$('document').ready(function(){
        $.ajax({
          url: "https://somesite.com/somefolder",
          type: "POST",
          dataType: 'jsonp',
          beforeSend: function(xhr) {
                xhr.setRequestHeader ("Authorization", "Basic " + btoa('myusername' + ":" + 'mypassword'));
            },
          success: function(data){
            console.log('SUCCESS!');
          },
          error: function () {
            console.log("error");
            }
        });         
});

因此,我在beforeSend中提供了憑據,因此我的期望是瀏覽器不會彈出憑據,因為我已經提供了憑據,但是不幸的是,當我運行此代碼時,我得到了彈出窗口以輸入憑據。 我想在代碼中提供這些憑據。

此問題類似,使用標頭的示例:

    $.ajax({
      url: "https://somesite.com/somefolder",
      type: "POST",
      dataType: 'jsonp',
      headers: {"Authorization": "Basic " + btoa('myusername' + ":" + 'mypassword')},
      success: function(data){
        console.log('SUCCESS!');
      },
      error: function () {
        console.log("error");
        }
    }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM