将基本身份验证与jQuery和ajax结合使用。我试图通过浏览器创建一个基本的身份验证,但我无法真正做到这一点。如果这个脚本不在这里,浏览器身份验证将接管,但我想告诉浏览器,用户即将进行身份验证。地址应该是:http://username:password@server.in.local/我有一份表格:<form name="cookieform" id="login" method="post">
<input type="text" name="username" id="username" class="text"/>
<input type="password" name="password" id="password" class="text"/>
<input type="submit" name="sub" value="Submit" class="page"/></form>还有一个剧本:var username = $("input#username").val();var password = $("input#password").val();function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = Base64.encode(tok);
return "Basic " + hash;}$.ajax ({
type: "GET",
url: "index1.php",
dataType: 'json',
async: false,
data: '{"username": "' + username + '", "password" : "' + password + '"}',
success: function (){
alert('Thanks for your comment!');
}});
4 回答
RISEBY
TA贡献1856条经验 获得超5个赞
beforeSend
beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));},
千巷猫影
TA贡献1829条经验 获得超7个赞
xhr.setRequestHeader
$.ajax
$.ajax({ type: "GET", url: "index1.php", dataType: 'json', username: username, password: password, data: '{ "comment" }', success: function (){ alert('Thanks for your comment!'); }});
401 Unauthorized
setRequestHeader
'headers'
:
$.ajax({ type: "GET", url: "index1.php", dataType: 'json', headers: { "Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD) }, data: '{ "comment" }', success: function (){ alert('Thanks for your comment!'); }});
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
var username = $("input#username").val();var password = $("input#password").val(); function make_base_auth(user, password) { var tok = user + ':' + password; var hash = btoa(tok); return "Basic " + hash;}$.ajax ({ type: "GET", url: "index1.php", dataType: 'json', async: false, data: '{}', beforeSend: function (xhr){ xhr.setRequestHeader('Authorization', make_base_auth(username, password)); }, success: function (){ alert('Thanks for your comment!'); }});
牛魔王的故事
TA贡献1830条经验 获得超3个赞
添加回答
举报
0/150
提交
取消