使用JavaScript在IP上获取国家/地区的访问者

使用JavaScript在IP上获取国家/地区的访问者,javascript,frontend,currency,Javascript,Frontend,Currency,我想使用javascript根据访问者的IP获取他们所在国家的信息。我在html代码中做了一些更改。我在stackoverflow上找到了一些代码,效果很好,但我不知道如何从数组中提取国家 代码 $.get("https://api.ipdata.co", function (response) { $("#response").html(JSON.stringify(response, null, 4)); }, "jsonp"); <script src="https://aj

我想使用javascript根据访问者的IP获取他们所在国家的信息。我在html代码中做了一些更改。我在stackoverflow上找到了一些代码,效果很好,但我不知道如何从数组中提取国家

代码

$.get("https://api.ipdata.co", function (response) {
    $("#response").html(JSON.stringify(response, null, 4));
}, "jsonp");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="response"></pre>
$.get(“https://api.ipdata.co“,功能(响应){
$(“#response”).html(JSON.stringify(response,null,4));
}“jsonp”);
我想要什么:

我的html页面上有一个定价表,现在我只想根据访问者的国家更改价格符号


非常感谢您的帮助。

使用
fetch
并完全避免jQuery怎么样

fetch('https://api.ipdata.co')
.then(res => res.json())
.then(data => console.log(data.country_code));

编辑:将Id为
response
的元素的文本值更改为返回的货币符号

fetch('https://api.ipdata.co')
.then(res => res.json())
.then(data => {
    document.querySelector('#response').textContent = data.currency.symbol;
});
检查这是否有效:

$.get("https://api.ipdata.co", function (response) {
    $("#response").html(response.currency.symbol);
}, "jsonp");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="response"></pre>
$.get(“https://api.ipdata.co“,功能(响应){
$(“#response”).html(response.currency.symbol);
}“jsonp”);

您提供的代码的作用是什么?您希望它做什么以及如何实现它?
response
不是一个数组,而是一个对象,该对象有两个与国家/地区相关的属性
country\u name
country\u code
。那么,您面临的确切问题是什么呢?在get请求中,我没有看到您向端点发送任何IP地址来检索国家,因此我们不知道您的代码是什么。请再次阅读问题。这会将国家货币符号($例如)作为HTML附加到ID中。希望您能实现这一点。如何将其注入我的html,比如如何使用我从“fetch(”)中得到的响应更改货币符号。然后(res=>res.json())。然后(data=>console.log(data.country_code));'