0

This code works fine in the development machine and over http but when I change to https stops working Any help will be greatly appreciated

The code is from zippopotamus is use to get the city and state displayed in their respective input fields by first inserting the zipcode in its input field here I have a 2 div's that are hidden citybox statebox

when you insert the correct zip these boxes will show with the citybox and statebox

Edit The working code its now displayed here Thanks to all here and the suggestion provided by Anders Changing from http to https

$(document).ready( function()  {

  $("#citybox").hide();
  $("#statebox").hide();


  $('input#zip').bind("change keyup input",function() {
    var zip_in = $(this);
    var zip_box = $('#zipbox');

    if (zip_in.val().length<5)
    {

      zip_box.removeClass('has-error has-success');
    }
    else if ( zip_in.val().length>5)
    {

      zip_box.addClass('has-error').removeClass('has-success');
    }

    else if ((zip_in.val().length == 5) ) 
    {

      var urls =["https://api.zippopotam.us/us/" ,"https://api.zippopotam.us/pr/","https://api.zippopotam.us/vi/"];

      $.each(urls, function(i,u){ 
        $.ajax(u + zip_in.val(),{
          cache: false,
          dataType: 'json',
          type: 'GET',
          success: function(result, success) {
            // Make the city and state boxes visible

            $('#citybox').slideDown();
            $('#statebox').slideDown();

            // US Zip Code Records Officially Map to only 1 Primary Location for  abbreviation
            places = result['places'][0];
            $('#city').val(places['place name']);
            $('#state').val(places['state']);
            zip_box.addClass('has-success').removeClass('has-error');
          },
          error: function(result, success) {
            zip_box.removeClass('has-success').addClass('has-error');

          }
        });
      });
    }
  });
});
1
  • 1
    you can't load http resources from an https page Commented Mar 4, 2016 at 23:07

3 Answers 3

1

Zippopotam.us supports https for it's api. Change the urls to "https://api.zippopotam.us/" and it should work.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Anders and everyone else I did change to https
Sorry I pressed the enter key
did it again, I did change to https still not showing the results expected and now the hidden div's are always visible, I will read what fred suggested (Same origin policy, but if anyone has other suggestions I will welcome them, Again Thank you All
Thanks Anders, after looking at my code I accidentally had change some other part of the code, Thanks to you and everyone else here I got it working
1

The problem is because you're reaching an insecure resource (http://api.zippopotam.us/us/) from a secure environment (your site running on https). This is not allowed. Your options are:

  • check if this server also runs on https;
  • write some back-end code to invoke this service, working like a proxy;
  • change to a vendor that supports https.

Comments

0

Take a look at the Same-Origin Policy.

You can't make a request to another domain via HTTP if your site is running under HTTPS.

Thus you should use API via HTTPS or as @Jodevan said use your server as a proxy.

1 Comment

Thanks Fred I found while reading that I can also use XMLHttpRequest , but I had a mistake in my code just fix it, Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.