Mario Burga
Mario Burga

Reputation: 1157

When I redirect to a page, depending on the country it does not work in https

I have the following code, where according to the country I will redirect to a certain page. This code works correctly for me in http but if I load the html page usinghttps it does not do anything.

<html>
 <head>
        <script src="//code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
        <script>
              jQuery.ajax( {
              url: '//api.ipstack.com/181.64.157.39?access_key=xxxx',
              type: 'POST',
              dataType: 'jsonp',
              success: function(location) {
                // If the visitor is browsing from Canada.
                if (location.country_code === 'CA') {
                  // Redirect him to the Canadian store.
                  //window.top.location.href = 'http://google.com.pe';
                  document.location.href = 'http://google.ca';
                }
              }
            } );
        </script>

 </head>
 <body>
<h2>Hello</h2>


 </body>
</html>

Why does this happen, how do I make it work in both?

Upvotes: 0

Views: 1544

Answers (1)

Ngorld
Ngorld

Reputation: 856

Your code is not working in HTTPS because your ipstack Plan API not support HTTPS. So if you using HTTPS to request, you will return error :

{"success":false,"error":{"code":105,"type":"https_access_restricted","info":"Access Restricted - Your current Subscription Plan does not support HTTPS Encryption."}}

So you should edit your AJAX request to force using HTTP instead :

<script>
      jQuery.ajax( {
      url: 'http://api.ipstack.com/181.64.157.39?access_key=xxxx',
      type: 'POST',
      dataType: 'jsonp',
      success: function(location) {
        // If the visitor is browsing from Canada.
        if (location.country_code === 'CA') {
          // Redirect him to the Canadian store.
          //window.top.location.href = 'http://google.com.pe';
          document.location.href = 'http://google.ca';
        }
      }
    } );
</script>

Upvotes: 1

Related Questions