How to prevent browser to invoke basic auth popup and handle 401 error using Jquery?

后端 未结 11 1258
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 12:18

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

11条回答
  •  误落风尘
    2020-11-27 12:53

    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:

    • Change the server response to not return a 401. Return a 200 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"
      }
      

    Please also take a look at this for an example of using jQuery with Basic Auth.

提交回复
热议问题