通过smartip.io获取IP填写Form

Getting IP to fill in Form through smartip.io

我在获取 IP 信息以填写隐藏的表单字段时遇到一些问题。

到目前为止,我使用的是免费服务 ipinfo.io,但是因为我每天收到的回复太多,所以这个免费网站是不够的。因此,我将不得不购买更好的服务。

这是我用到今天的代码:

<script type="text/javascript">

$.get("https://ipinfo.io", function (response) {
    $("#IPnaslov").html("IP: " + response.ip);
    $("#address").html("Location: " + response.city + ", " + response.region);
    $("#details").html(JSON.stringify(response, null, 4));
    $("#city").html(response.city);
    document.getElementById('ip').value = response.ip;
}, "jsonp");
</script>

<form>
<input name="IPnaslov" type="hidden" id='ip'type="hidden">
  <br><button type="submit" class="btn btn-outline-danger" name="submitted" value="true">Submit</button>
</form>

市面上有很多IP服务,我不知道该用哪一个。我试过 https://smartip.io/ 因为它可以免费注册最多 125000 个请求。

有没有一种方法可以让我像使用 ipinfo.io 一样使用他们的服务?

再次感谢您的提前帮助。

首先,当访问者不是人类时,您不应该调用 API。这样可以节省一些请求。

if (navigator.userAgent.match(/bot|spider/i)) {
    // It is a bot, probably keep it going to crawl your site
} else {
    // It's not a bot, hit the API:
    $.get("https://api.smartip.io/[client_ip]?api_key=[YOUR_API_KEY]", function(response) {
        console.log("response");
    });
}

不管怎样,那个服务需要你传递IP。您不能仅使用 javascript 获取 IP。您必须在服务器端进行。

您应该使用任何其他 from this answer

就个人而言,我使用 geoPlugin

这应该适合你:

$.getJSON('http://www.geoplugin.net/json.gp?jsoncallback=?', function(data) {
  $("#IPnaslov").html("IP: " + data.geoplugin_request);
    $("#address").html("Location: " + data.geoplugin_city + ", " + data.geoplugin_region);
    $("#details").html(JSON.stringify(data, null, 4));
    $("#city").html(data.geoplugin_city);
    document.getElementById('ip').value = data.geoplugin_request;
});