如何防止浏览器调用基本身份验证弹出窗口并使用 Jquery 处理 401 错误? [英] How to prevent browser to invoke basic auth popup and handle 401 error using Jquery?
问题描述
我需要使用基本身份验证发送授权请求.我已经使用 jquery 成功实现了这一点.但是,当我收到 401 错误时,基本身份验证浏览器弹出窗口已打开并且未调用 jquery ajax 错误回调.
I need to send authorization request using basic auth. I have successfully implemented this using jquery. However when I get 401 error basic auth browser popup is opened and jquery ajax error callback is not called.
推荐答案
我最近也遇到了这个问题.由于您无法更改浏览器在 401
(basic 或 digest 身份验证)情况下显示弹出窗口的默认行为,因此有两个解决方法:
I was facing this issue recently, too. Since you can't change the browser's default behavior of showing the popup in case of a 401
(basic or digest authentication), there are two ways to fix this:
- 将服务器响应更改为不返回
401
.返回一个200
代码并在您的 jQuery 客户端中处理它. 将您用于授权的方法更改为标头中的自定义值.浏览器将显示Basic 和Digest 的弹出窗口.您必须在客户端和服务器上更改此设置.
- Change the server response to not return a
401
. Return a200
code instead and handle this in your jQuery client. Change the method that you're using for authorization to a custom value in your header. Browsers will display the popup for Basic and Digest. You have to change this on both the client and the server.
headers : {
"Authorization" : "BasicCustom"
}
另请查看这个以基本身份验证使用 jQuery 的示例.
Please also take a look at this for an example of using jQuery with Basic Auth.
这篇关于如何防止浏览器调用基本身份验证弹出窗口并使用 Jquery 处理 401 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!