【发布时间】: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