Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
443 views
in Technique[技术] by (71.8m points)

javascript - 对jQuery和Ajax使用基本身份验证(Use basic authentication with jQuery and Ajax)

I am trying to create a basic authentication through the browser, but I can't really get there.(我正在尝试通过浏览器创建基本身份验证,但我真的无法到达那里。)

If this script won't be here the browser authentication 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:[email protected]/ 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 + ':' + 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!'); } });   ask by Patrioticcow translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use jQuery's beforeSend callback to add an HTTP header with the authentication information:(使用jQuery的beforeSend回调添加带有身份验证信息的HTTP标头:)

beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password)); },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...