如何防止浏览器调用基本的auth弹出窗口并使用Jquery处理401错误? [英] How to prevent browser to invoke basic auth popup and handle 401 error using Jquery?
问题描述
我需要使用基本身份验证发送授权请求。我已经使用jquery成功实现了这一点。但是当我得到401错误时,基本的auth浏览器弹出窗口被打开并且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
(基本或摘要>的情况下,您无法更改浏览器显示弹出窗口的默认行为身份验证),有两种方法可以解决这个问题:
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的示例,请参阅此使用Basic Auth。
Please also take a look at this for an example of using jQuery with Basic Auth.
这篇关于如何防止浏览器调用基本的auth弹出窗口并使用Jquery处理401错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!