javascript 我们如何使用jquery通过ip地址获取国家名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28187054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:32:22  来源:igfitidea点击:

how can we get country name through ip address using jquery

javascriptjqueryhtmlcss

提问by user3446322

Please help me to find the country name through ip address using jquery or JavaScript.

请帮助我使用 jquery 或 JavaScript 通过 IP 地址查找国家/地区名称。

I am using this code for getting the ip address. But I am not getting any idea how can we get the country name using the IP Address. Please help me.

我正在使用此代码来获取 IP 地址。但是我不知道如何使用 IP 地址获取国家/地区名称。请帮我。

<script>
    $.getJSON("http://jsonip.com?callback=?", function (data) {
     alert("Your ip: " + data.ip);
 });
</script>

Please help me

请帮我

回答by adeneo

Use a service that actually returns countrynames, like FreeGeoIp

使用实际返回国家/地区名称的服务,例如 FreeGeoIp

$.getJSON("http://freegeoip.net/json/", function (data) {
    var country = data.country_name;
    var ip = data.ip;
});