绕过 jQuery 和 AJAX 中的 SSL 证书错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25723415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:09:29  来源:igfitidea点击:

Bypassing SSL certificate error in jQuery and AJAX

jqueryajaxssl

提问by ρss

I am newbie to jQuery/AJAX. I have a small application for testing pupose that has a button on it. When the button is clicked a connection is made to a server located in the same domain to get some data and alert it.

我是 jQuery/AJAX 的新手。我有一个用于测试目的的小应用程序,上面有一个按钮。单击按钮时,将连接到位于同一域中的服务器以获取一些数据并提醒它。

Problem: My application can't make any connection to the server. The following screen shot is from the developer tools in google chrome.

问题:我的应用程序无法与服务器建立任何连接。以下屏幕截图来自 google chrome 中的开发人员工具。

eror

错误

The server has its own self signed certificate. If I connect to the server via web browser I get an SSL certificate warning as shown below.

服务器有自己的自签名证书。如果我通过 Web 浏览器连接到服务器,则会收到 SSL 证书警告,如下所示。

ssl-error

ssl错误

If I click on proceed and then login to the server, after this now my application is also able to retieve the data from the server.(If I click the button on my web app it alerts the data it got from the server.)

如果我单击继续然后登录到服务器,在此之后,我的应用程序现在也可以从服务器获取数据。(如果我单击 Web 应用程序上的按钮,它会提醒从服务器获取的数据。)

Question:Is there any workaround for this, can I bypass this error? Why it works once I have logged in to the server via web browser? My app will be used locally in the same domain and it is not a public app.

问题:是否有任何解决方法,我可以绕过此错误吗?为什么我通过网络浏览器登录到服务器后它会工作?我的应用程序将在同一个域中本地使用,它不是公共应用程序。

jQuery Code: I have this code:

jQuery 代码:我有这个代码:

$('#mybutton').click(function(){
    $.ajax({
        type: "GET",
        url: "https://192.168.150.33/Api/loc?jsonpCallback=myCallback",
        dataType:"jsonp",
        async: false,
        beforeSend: function (request){
            request.withCredentials = true;
            request.setRequestHeader("Authorization", "Basic " + btoa('admin' + ":" + 'password'));
            },
        success: function(response){
                alert('hi');
            },
    });
});
function myCallback(response){
        data= JSON.stringify(response)
        alert(data)

Hereis a post that addresses the same issue. As far as I understood this post according to it there is no solution. Any suggestions will be helpful. Thanks

是解决相同问题的帖子。据我了解this post根据它没有解决方案。任何建议都会有所帮助。谢谢

回答by Alex K.

You cannot programmatically bypass the SSL error/warning behaviour implemented by the browser, if you could it would invalidate that security layer entirely.

您不能以编程方式绕过浏览器实现的 SSL 错误/警告行为,如果可以的话,它会使该安全层完全无效。

If you are doing this locally/in a Windowsdomain environment simply add the self signed cert to the trusted store.

如果您在本地/在Windows域环境中执行此操作,只需将自签名证书添加到受信任的存储

Additionally a certificate is (typically) issued to a domain name not an IP address so you will need to do the same in your Ajax call.

此外,证书(通常)颁发给域名而不是 IP 地址,因此您需要在 Ajax 调用中执行相同操作。