Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

jquery - Using following Javascript (Ajax) code, working fine on http but not working on https

I am using the below code for redirection, if the user's country is not India then redirect it else keep on the same page

<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
   <script type="text/javascript">
   
    function preloadFunc()
    {
        $.ajax('http://ip-api.com/json')
  .then(
      function success(response) {
        if(response.country!="India")
        {window.location.replace("https://www.google.com/");}
    }
    window.onpaint = preloadFunc();
</script>
question from:https://stackoverflow.com/questions/65949618/using-following-javascript-ajax-code-working-fine-on-http-but-not-working-on

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What happens when you try to do the http call from an https initiated site:

jquery-1.9.1.min.js:5 Mixed Content: The page at 'https://******' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://ip-api.com/json'. This request has been blocked; the content must be served over HTTPS.

If you try to use https for this call you get:

jquery-1.9.1.min.js:5 GET https://ip-api.com/json 403 (Forbidden)

and if you try https://ip-api.com/json direct in your browser you get

{"status":"fail","message":"SSL unavailable for this endpoint, order a key at https://members.ip-api.com/"}

Incidentally, you also have two JS syntax errors in your code. Here is a corrected version (not that it helps in getting the ip stuff returned over https I'm afraid).

<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
   
  function preloadFunc()
    {
      $.ajax('https://ip-api.com/json')
        .then(
      function success(response) {console.log(response);
        if(response.country!="India") {
          window.location.replace("https://www.google.com/");
        }
      })
    }
    window.onpaint = preloadFunc();
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...