[英]How to disable browser's default login prompt on 401 response?
Is there ANY way to suppress the browser's login prompt on 401 response when using XmlHttpRequest.使用 XmlHttpRequest 时,是否有任何方法可以在 401 响应中抑制浏览器的登录提示。
var req = new XMLHttpRequest(); req.open("POST", URL, true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function () { if (this.readyState == 4) { req.onreadystatechange = null; if (this.status == 200) { //alert("Action called successfully"); }else if(this.status == 401) { // } else { var error = JSON.parse(this.response); alert(error.Message); } } };
If the URL looks like this:如果 URL 如下所示:
http://www.example.com
You can pass the login credentials like this:您可以像这样传递登录凭据:
http://username:password@www.example.com/
Or you could try sending them in a header:或者您可以尝试在标题中发送它们:
req.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.