簡體   English   中英

如何使用基本身份驗證從服務器動態加載JavaScript文件

[英]How to dynamically load a javascript file from a server with Basic Authentication

我們有一個受基本身份驗證保護的網站,並在其下面有一個特定的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-在-ajax.html

您是否嘗試過標准的http://user:pass@hostname/語法?

暫無
暫無

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

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