Determine if ajax call failed due to insecure response or connection refused

后端 未结 6 2144
旧巷少年郎
旧巷少年郎 2020-11-29 21:59

I\'ve been doing a lot of research and could not find a way to handle this. I\'m trying to perform a jQuery ajax call from an https server to a locahost https server running

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 22:43

    Unfortunately the present-day browser XHR API does not provide an explicit indication for when the browser refuses to connect due to an "insecure response", and also when it does not trust the website's HTTP/SSL certificate.

    But there are ways around this problem.

    One solution I came up with to determine when the browser does not trust the HTTP/SSL certificate, is to first detect if an XHR error has occurred (using the jQuery error() callback for instance), then check if the XHR call is to an 'https://' URL, and then check if the XHR readyState is 0, which means that the XHR connection has not even been opened (which is what happens when the browser does not like the certificate).

    Here's the code where I do this: https://github.com/maratbn/RainbowPayPress/blob/e9e9472a36ced747a0f9e5ca9fa7d96959aeaf8a/rainbowpaypress/js/le_requirejs/public/model_info__transaction_details.js#L88

提交回复
热议问题