javascript - Shopify getJSON API call to get the countryCode -


i'm trying return country code ip address making json call [http://ip-api.com/json][1] [1]: http://ip-api.com/json , write currency code cookie, , update currency entire site.

the code have @ moment below (html , css jsfiddle , not part of code), works on jsfiddle cannot seem work in store on shopify.

$.getjson('http://ip-api.com/json', function (location) {        if (location.countrycode == 'au') {         $('#currencies').text("aud");          currency.cookie.write('aud');          currency.convertall(currency.currentcurrency, 'aud');      }       else {         $('#currencies').text("eur");          currency.cookie.write('eur');          currency.convertall(currency.currentcurrency, 'eur');      }  });
body {      font-size: 75%;      font-family:"segoe ui", verdana, helvetica, sans-serif;  }  #body {      clear: both;      margin: 0 auto;      max-width: 534px;  }  table {      border-collapse: collapse;      border-spacing: 0;      margin-top: 0.75em;      border: 0 none;      margin-top:35px;  }  #body td {      padding:15px 30px 15px 10px;  }  #body tr td:nth-child(1) {      font-weight:bold;  }  #address {      width:400px;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="body">      <table border="1">          <tr>              <td>currency:</td>              <td id="currencies"></td>          </tr>      </table>  </div>

this first time coding js , making json calls, i'm not sure if it's syntax error when using jquery in shopify or other issue? in advance.

i'm not sure if .text valid usage. try this

$.getjson('http://ip-api.com/json', function (location) {     if (location.countrycode == 'au') {        $('#currencies').html('aud');         currency.cookie.write('aud');         currency.convertall(currency.currentcurrency, 'aud');     }      else {        $('#currencies').html('eur');         currency.cookie.write('eur');         currency.convertall(currency.currentcurrency, 'eur');     } }); 

also imho localstorage better option cookies. - http://www.w3schools.com/html/html5_webstorage.asp

update discussion in comments:

$.getjson('https://geoip.nekudo.com/api/', function (location) {     if (location.country.code == 'au') {        $('#currencies').val('aud');         currency.cookie.write('aud');         currency.convertall(currency.currentcurrency, 'aud');     }      else {        $('#currencies').val('eur');         currency.cookie.write('eur');         currency.convertall(currency.currentcurrency, 'eur');     } }); 

Comments