![](/img/trans.png)
[英]Can I suppress the browser’s login prompt on 401 response when using XmlHttpRequest with Twitter
[英]How to disable browser's default login prompt on 401 response?
使用 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); } } };
如果 URL 如下所示:
http://www.example.com
您可以像這樣傳遞登錄憑據:
http://username:password@www.example.com/
或者您可以嘗試在標題中發送它們:
req.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.