使用 ajax 发送 ip 和接收位置

Sending ip and receiving location using ajax

我想知道我可以发送用户 IP 地址并接收他们的 country/place 作为 plain/text 的任何网站网页。

作为参考,我在下面的代码片段中进行了演示。

我尝试执行以下代码来获取用户 IP。

<script type="text/javascript" src="https://l2.io/ip.js?var=userip"></script>

...

<script type="text/javascript">
  document.write("Your IP is :", userip);
</script>

$.post("domain.sdm/page", {
    "ip": "123.234.345"
}, function(dat,suc) {
    alert("Your country is: "+ dat);
});

一个好的网站应该是 https://iplocation.com/

如果您在 POST 请求中发送 IP 地址,您将收到一个包含大量有关 IP 地址的信息的捆绑包,包括城市和国家/地区。

此外,IP 地址由 4 个字节组成,这意味着 4 个数字最多为 2^8,而不是 3。

!免责声明!由于 CORS 政策,您必须通过 https://cors-anywhere.herokuapp.com 传递 URL,否则您的请求将被阻止,因为您会从另一个域发出请求。

$.post("https://cors-anywhere.herokuapp.com/https://iplocation.com", {
    "ip": "124.84.42.76" //for example but watch out some ip addresses might not exist
}, function(dat,suc) {
    alert("Your country is: "+ dat.country_name);
});