2012-11-30 33 views
2

我們有一個受基本認證保護的網站,並且在其下有一個特定的JavaScript文件。如何從具有基本認證的服務器動態加載JavaScript文件

我想通過在加載文件時在驗證標頭上加載詳細信息從不同的MVC3站點加載該JavaScript文件,以下是我正在嘗試的這似乎不起作用 - 它總是彈出基本身份驗證登錄對話框:

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 
<html> 
<head> 
    <title>Auth test</title> 
</head> 
<body> 
    <script src="~/Scripts/jquery-1.7.1.min.js"></script> 
    <script src="~/Scripts/Base64.js"></script> 
    <h2>Index</h2> 

    <script type="text/javascript"> 


     $(document).ready(function() { 

      var auth = make_base_auth('domain\user', 'password'); 

      $.ajaxSetup({ 
       headers: { 
        'Authorization': auth 
       } 
      }); 


      $.ajax({ 
       url: 'https://localhost/WebClient/scripts/bob.js', 
       dataType: 'script', 
       //username: 'domain\user', 
       //password: 'password', 
       //withCredentials: true, 
       //crossDomain: true, 
       //headers: { 'Authorization': auth }, 
       success: function (data) { 
        alert("Got the script!"); 

        callFunctionInTheOtherJSFile(); 
       }, 
       async: false 
      }); 


     }); 

     function make_base_auth(user, password) { 
      var tok = user + ':' + password; 
      var hash = Base64.encode(tok); 
      return "Basic " + hash; 
     } 

    </script> 
</body> 
</html> 

任何想法,我應該做些什麼來讓代碼工作?你會從各種註釋部分看到 - 我嘗試了幾種不同的方法(新舊jQuery方法等)!

作爲參考,Base64的JS就是從這裏取 - http://www.webtoolkit.info/javascript-base64.html這裏描述 - http://coderseye.com/2007/how-to-do-http-basic-auth-in-ajax.html

回答

1

您是否嘗試過的標準http://user:[email protected]/語法?

+0

是的 - 也沒有工作。儘管這可能是因爲我需要添加用戶的域名 - 例如http:// domain \ user:pass @ hostname/ – trembler2003

+0

請注意,該url也在https下,如果這有什麼區別 – trembler2003