Pure JavaScript code for HTTP Basic Authentication?

こ雲淡風輕ζ 提交于 2019-12-17 05:40:47

问题


Where can I find reference code that implements a HTTP Basic Authentication client in pure JavaScript, suitable for AJAX?

Extra points for code, or pointers to code, that can be used independent of JS toolkits like YUI. No points for Java, Flash/Flex, PHP frameworks, etc.


回答1:


The five-parameter version of the XMLHttpRequest.open method allows you to specify the username and password. (WHATWG spec)

xhr.open(method, url, async, username, password)



回答2:


There is a good article/tutorial written by Paul James. I used it some time ago and it worked for me.

HTTP Authentication with HTML Forms

[...] XMLHTTPRequest, it can submit the correct HTTP auth headers for us. Rather than adjusting the URL the form submits to, we can use XMLHTTPRequest to do a request before the form submits supplying the entered username and password.

This will set up the browser with the HTTP auth credentials so it'll also send them with our actual form submission login request.



来源:https://stackoverflow.com/questions/491914/pure-javascript-code-for-http-basic-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!