使用 javascript 控制 HTTP 基本身份验证
我想使用 javascript 检查 URL 是否受到 Http 基本身份验证的保护。 这是我到目前为止所得到的:
function showFailure(){ alert("FAILED ") }
function showSuccess(){ alert("SUCCESS") }
var myRequest = new Request({
url: 'http://localhost/access_protected_HTTP_BASIC',
method: 'get',
onSuccess: showSuccess,
onFailure: showFailure
}).send();
但这实际上打开了浏览器登录弹出窗口来访问资源。 有没有办法不触发该弹出窗口?
谢谢!
注意:我在这个例子中使用了 mootools,但我会采用任何可以实现这一点的 javascript 示例:)
I'd like to check if a URL is protected by a Http Basic Authentication using javascript.
Here is what I have so far :
function showFailure(){ alert("FAILED ") }
function showSuccess(){ alert("SUCCESS") }
var myRequest = new Request({
url: 'http://localhost/access_protected_HTTP_BASIC',
method: 'get',
onSuccess: showSuccess,
onFailure: showFailure
}).send();
But that actually opens the browser login popup to access the resource.
Is there a way not to trigger that popup?
Thanks!
Note : I use mootools in this example but I'd take any javascript example that does the trick :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据类似问题的答案,您可以手动传递发送请求时的用户名和密码。 (根据 MooTools 文档,
用户
和password
参数正是这样做的。)此外,
XMLHttpRequest
规范 说:这意味着您可以设置虚拟用户名和密码,浏览器不会提示用户。如果返回401状态码,则表示需要授权。
Based on the answer to a similar question, you can manually pass a username and password when sending a request. (And according to the MooTools Docs, the
user
andpassword
parameters do exactly this.)Further, the
XMLHttpRequest
spec says:This means you can set a dummy username and password, and the browser won't prompt the user. If you get back a 401 status code, it means authorization is required.
您需要做的就是添加一个标头
Authorization: Basic *Base64EncodedString*
,其中*Base64EncodedString*
是base64Encode(username+':'+password)
> 应该很容易找到一个 base64encode 函数。青年MMVall you should need to do is add a header
Authorization: Basic *Base64EncodedString*
where*Base64EncodedString*
isbase64Encode(username+':'+password)
it should be easy enough to find a base64encode function out there. YMMV