本文介绍了如何使用基本身份验证与jQuery和AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建通过浏览器基本身份验证,但我真的不能到那里。
如果该脚本将不会在这里浏览器的权威性将接管,但我想告诉用户即将进行验证的浏览器。
地址应该是这样的:
的http://用户名:password@server.in.local/
我有一个表格:
<表格名称=cookieformID =登录方法=后>
<输入类型=文本名称=用户名ID =用户名级=TEXT/>
<输入类型=密码NAME =密码ID =密码级=TEXT/>
<输入类型=提交名称=分值=提交级=页面/>
< /表及GT;
和脚本:
VAR用户名= $(#输入用户名)VAL()。
。VAR密码= $(#输入密码)VAL();功能make_base_auth(用户名,密码){
VAR TOK =用户+:+通过;
VAR哈希= Base64.en code(TOK);
返回基本+散;
}
$就
({
键入:GET,
网址:index1.php
数据类型:JSON,
异步:假的,
数据:{用户名:'+用户名+',密码:'+密码+'}
成功:函数(){
警报('感谢您的评论!);
}
});
解决方案
使用jQuery的回调与认证信息添加一个HTTP头:
beforeSend:功能(XHR){
xhr.setRequestHeader(授权,基本+ BTOA(用户名+:+密码));
},
I am trying to create a basic auth through browser, but I can't really get there.
If this script won't be here the browser auth will take over, but I want to tell the browser that the user is about to make the authentication.
The address should be something like:
http://username:password@server.in.local/
I have a form:
<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>
And a script:
var username = $("input#username").val();
var password = $("input#password").val();
function make_base_auth(user, password) {
var tok = user + ':' + pass;
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!');
}
});
解决方案
Use jQuery's beforeSend
callback to add an HTTP header with the authentication information:
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
},
这篇关于如何使用基本身份验证与jQuery和AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!