我在IIS中的網站,它有一個名爲「集裝箱/文件」,這需要基本身份驗證來訪問文件夾,該文件夾的文件都會有一些8到9個用戶將通過我的網站下載的文件。另外我想在應用程序中存儲用戶名和密碼,所以不想看到用於身份驗證的窗口對話框。
我做了什麼:
我已經寫了,給了我訪問該文件夾,並使用我的用戶ID和密碼列出的所有文件Ajax請求,但是當我點擊要下載的文件,它再次要求我進行身份驗證。
我想實現:
如何繞過這個文件級別的身份驗證用戶已經通過身份驗證才能訪問這個文件夾,爲什麼他不能只是下載的文件?
我的代碼:
$(function() {
var id = "ss221",
pass = "test12",
url = "container/files/",
$s = $(".shell ul");
// Setup our method to retrieve the file list
var getFiles = function() {
$.ajax({
url: url,
type: 'GET',
headers: {
"Authorization": "Basic " + btoa(id + ":" + pass)
},
success: function(data) {
alert('ok');
var links = $(data).find("a");
// for each item in links...
links.each(function(l) {
// extract the href attr
var href = $(this).attr("href");
$s.append("<li><a target='_blank' href=\"" + href + "\">" +
$(this).text() + "</a></li>");
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("error");
}
});
};
// assign my click handler to #btnGet
$("#btnGet").click(function() {
$(".shell ul").empty();
getFiles();
});
});
.shell {
width: 300px;
}
.shell ul li {
list-style: none;
}
.shell a {
color: #232323;
display: block;
width: 100%;
height: 30px;
margin: .5em 0;
padding: 5px 10px;
font-weight: bold;
border: 1px solid #999;
border-radius: 3px;
text-decoration: none;
}
<!doctype html>
<html>
<meta charset="utf-8">
<head>
<style type="text/css">
</style>
</head>
<body>
<div>
List All files here...
</div>
<button id="btnGet">
get</button>
<div class="shell">
<!-- This is the place we're going to build our list -->
<ul>
</ul>
</div>
</body>
<!-- Get us some jQuery goodness! -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js"></script>
<script type="text/javascript">
</script>
</html>
我使用基本身份驗證。 –