在jQuery和AJAX绕过SSL证书错误证书、错误、jQuery、AJAX

2023-09-11 01:18:20 作者:半生輕狂客

我是新手的jQuery / AJAX。我有一个小的应用程序进行测试章节目标是上有一个按钮。当按钮被点击建立连接到位于在同一个域中得到一些数据,并提醒它的服务器

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.

问题:我的应用不能到服务器的任何连接。下面的屏幕截图是从谷歌Chrome开发者工具。

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

该服务器都有自己的自签名证书。如果我连接到通过Web浏览器的服务器我得到一个SSL证书警告,如下图所示。

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.

如果我点击继续,然后登录到服务器,在此之后,现在我的应用程序也能够从服务器retieve的数据。(如果我点击我的web应用程序的按钮,它会提醒它从服务器得到的数据。)

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 :我有这个code:

jQuery Code: I have this code:

$('#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)

Here是一个职位,解决了同样的问题。据我根据它的理解这篇文章有没有办法解决。任何建议将是有益的。谢谢

Here is 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

推荐答案

您不能以编程方式绕过由浏览器来实现,如果你可以将无效的安全层完全的SSL错误/警告行为。

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

如果你这样做是在本地/中的的Windows 的域环境中只需添加签名的证书的trusted商店。

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

此外证明书(典型值)发给域名而非IP地址,所以你需要在你的Ajax调用做同样的。

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.