0

I want to do a website button that redirects to specific webpages based on user location. I'm using this free API ( https://ipgeolocationapi.com/ ) and did the button logic with ifs but I get this error in console "..been blocked by CORS policy: No 'Access-Control-Allow-Origin'. Any thoughts on this? I'm a JS newbie and this is my first post. Thank you for your help.

function SubmitFrm() {
  $.getJSON("https://api.ipgeolocationapi.com/geolocate", function (data) {
    var country = data.alpha2;

    if (country == "SE") {
      location.href = "https://www.nmbrs.com/se";
    }

    if (country == "NL") {
      location.href = "https://www.nmbrs.com/nl";
    } else {
      location.href = "https://www.nmbrs.com/diogo";
    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="submit" name="btnSearch" value="Test" id="btnSearch" class="btn" onclick="javascript:SubmitFrm()" />
luxo
  • 1
  • 2
  • 1
    Welcome to Stack Overflow. CORS policy is a browser security protection. Take a look: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS – Twisty Apr 13 '20 at 15:06
  • If this API does not return CORS headers in the response then you will have to call it from the server side, not client side JS – Rory McCrossan Apr 13 '20 at 15:08

0 Answers0