如何使用jQuery获取客户端IP地址 [英] How to get client IP address using jQuery
问题描述
我想知道如何使用jQuery获取客户端IP地址?
有可能吗?我知道纯JavaScript不能,但从Stack Overflow本身使用 JSONP
获得一些代码。
所以,是有没有使用jQuery的解决方法?
jQuery可以处理JSONP,只需传递一个用callback =格式化的url?参数到 $。getJSON
方法,例如:
$。getJSON(https://api.ipify.org/?format=json,function(e){console.log(e.ip);});
< script src =https://ajax.googleapis.com /ajax/libs/jquery/2.1.1/jquery.min.js\"></script>
此示例是使用 api.ipify.org
实现的非常简单的JSONP服务。
如果你不是在寻找跨域解决方案,那么脚本可以更加简化,因为你不需要callback参数,并且你返回纯JSON。
I want to know how to get client IP address using jQuery?
Is it possible? I know pure javascript can't, but got some code using JSONP
from Stack Overflow itself.
So, is there any workaround using jQuery?
jQuery can handle JSONP, just pass an url formatted with the callback=? parameter to the $.getJSON
method, for example:
$.getJSON("https://api.ipify.org/?format=json", function(e) {
console.log(e.ip);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
This example is of a really simple JSONP service implemented on with api.ipify.org
.
If you aren't looking for a cross-domain solution the script can be simplified even more, since you don't need the callback parameter, and you return pure JSON.
这篇关于如何使用jQuery获取客户端IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!