Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bypassing SSL certificate error in jQuery and AJAX

Tags:

jquery

ajax

ssl

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.

Problem: My application can't make any connection to the server. The following screen shot is from the developer tools in 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.

ssl-error

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.)

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:

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

like image 953
ρss Avatar asked Sep 06 '25 03:09

ρss


2 Answers

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

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

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.

like image 138
Alex K. Avatar answered Sep 07 '25 20:09

Alex K.


I could solve this problem adding this to my ajax call:

beforeSend: function (request) {
    request.withCredentials = false;
}

Isn't a recommended practice...

like image 42
Enzo Gerola Avatar answered Sep 07 '25 19:09

Enzo Gerola