【问题标题】:Shopify getJSON API call to get the countryCodeShopify getJSON API 调用以获取 countryCode
【发布时间】:2016-04-06 13:40:36
【问题描述】:

我试图通过对 [http://ip-api.com/json][1] [1]: http://ip-api.com/json 进行 JSON 调用从 IP 地址返回国家代码,然后我想将货币代码写入 cookie,然后更新整个网站的货币。

我目前拥有的代码如下(HTML 和 CSS 来自 jsfiddle,而不是代码的一部分),它适用于 jsfiddle,但我似乎无法让它在我的 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>

这是我第一次使用 JS 编码并进行 JSON 调用,所以我不太确定在 Shopify 中使用 jQuery 时这是语法错误还是其他问题? 提前感谢您的帮助。

【问题讨论】:

    标签: javascript jquery json api shopify


    【解决方案1】:

    我不确定.text 是否有效。试试这个

    $.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');
        }
    });
    

    另外,恕我直言localStorage 是 cookie 的更好选择。 - http://www.w3schools.com/html/html5_webstorage.asp

    来自 cmets 的讨论更新:

    $.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');
        }
    });
    

    【讨论】:

    • 感谢关于 localstorage 的建议,很遗憾我尝试了您的代码,但没有成功。我进一步研究了 innerHTML 函数以了解它的作用。谢谢
    • 您能否分享您在浏览器控制台中看到的错误(如果有)?
    • 因为它在 Shopify 中没有显示错误,我认为这是一个反复试验的案例。我通过编辑代码然后刷新我的网站进行测试。
    • 试试$('#currencies').html('AUD')$('#currencies').html('EUR'),让我知道它是否有效。我错误地混合了 JS 和 jQuery。
    • 我试了一下,但没有运气。以前,当我使用 freegeoip.net 时,它与 .val 一起使用,但与 ip-api.com 一起使用时,我很难让它发挥作用。
    猜你喜欢
    • 2012-08-20
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多