702*_*2cs 4 html javascript jquery sample ip-geolocation
我无法弄清楚如何从访问我网页的访问者获取国家/地区代码.我见过很多其他的例子但没有使用普通的Javascript.这是我目前的代码:
<!DOCTYPE html>
<html lang="en-US">
<head>
</head>
<body>
<script type="text/javascript">
var userip;
</script>
<script type="text/javascript" src="https://l2.io/ip.js?var=userip"> </script>
<script type="text/javascript">
document.write("IP: ", userip);
$.get("http://ipinfo.io/"+userip.text(), function (response) {
reg.text("country code here" + response.country);
}, "jsonp");
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我从JSONP请求获取用户的IP地址到ipinfo.io使用$.getjQuery方法.
不幸的是l2.io.js,此时不再返回ip以外的任何内容.
如果有人可以提供帮助或有任何示例,请链接所有相关的JavaScript库.谢谢.
根据网站上的文档,您应该能够使用ipinfo检索国家/地区代码.尝试使用$ .getJSON而不是$ .get
var country_code = null;
$.getJSON('http://ipinfo.io/' + userip, function(data){
country_code = data.country;
alert(country_code);
});
Run Code Online (Sandbox Code Playgroud)
小智 6
我使用 jQuery 使用了下面的代码,它对我来说工作得很好。我得到了country_code,country_name等。
jQuery(document).ready(function($) {
jQuery.getScript('http://www.geoplugin.net/javascript.gp', function()
{
var country = geoplugin_countryName();
alert(country);
var code = geoplugin_countryCode();
alert(code);
console.log("Your location is: " + country + ", " + zone + ", " + district);
});
});
Run Code Online (Sandbox Code Playgroud)
注意:请记住导入插件脚本,如下所示:
<script src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
| 归档时间: |
|
| 查看次数: |
20648 次 |
| 最近记录: |