Javascript 使用javascript获取IP地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34584958/
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
Get ip address using javascript
提问by Bryan Rance
I just want to ask, is there a way on how to get ip addresses using javascript only? been searching for quite a while now and most of the results were I need to use api(s). I have used this webrtc and it works great but it's not working on IE, API is great, I've tested some and that works great in different browsers.
我只是想问,有没有办法只使用javascript获取ip地址?现在已经搜索了很长一段时间,大多数结果都是我需要使用 api(s)。我使用过这个 webrtc,它运行良好,但它不适用于 IE,API 很棒,我已经测试了一些并且在不同的浏览器中运行良好。
but I need to get the code itself from api, or is it possible to get/extract the code from api itself and make a specified file for the source so I won't rely on source from the internet?
但我需要从 api 获取代码本身,或者是否可以从 api 本身获取/提取代码并为源制作指定的文件,这样我就不会依赖来自互联网的源?
I need the RAW file from api, because if ever the src of the api went down, my site will be affected too, so I want it to get and create a external source and include it on my site.
我需要来自 api 的 RAW 文件,因为如果 api 的 src 出现故障,我的站点也会受到影响,所以我希望它获取并创建一个外部源并将其包含在我的站点中。
回答by Harsh Sanghani
Try following solution :-
尝试以下解决方案:-
First option :-
第一个选项:-
$(document).ready(function () {
$.getJSON("http://jsonip.com/?callback=?", function (data) {
console.log(data);
alert(data.ip);
});
});
Second option :-
第二种选择:-
$.get("http://ipinfo.io", function(response) {
alert(response.ip);
}, "jsonp");
It may help you.
它可能会帮助你。
回答by Anthony
I could be wrong, but I think you can only detect the IP serverside, so you'll have to do some kind of a get/post request.
我可能是错的,但我认为您只能检测 IP 服务器端,因此您必须执行某种获取/发布请求。
The other answer shows a possible implementation of this.
另一个答案显示了一个可能的实现。
Also, see this question: How to get client's IP address using javascript only?
另外,请参阅这个问题:How to get client's IP address only using javascript?
回答by gvozd1989
You need to create script at backend of your site, that will be return IP, and execute it via ajax.
您需要在站点后端创建脚本,该脚本将返回 IP,并通过 ajax 执行。
Or on the stage of generating page (at backend), you can detect IP, and put it to cookie, than read cookie from JS:
或者在生成页面阶段(后端),可以检测IP,并将其放入cookie,而不是从JS读取cookie:
function getCookie(name) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\/\+^])/g, '\') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
回答by Babar
If you want source data you can get it from MaxMind: http://dev.maxmind.com/geoip/There is a free and a paid version. Most of the IP info providers uses that library.
如果您需要源数据,可以从 MaxMind 获取:http://dev.maxmind.com/geoip/ 有免费和付费版本。大多数 IP 信息提供程序都使用该库。
If you only need the IP, you can create a script of your own. Just create a backend script something like with PHP and request it from JS. Example: http://php.about.com/od/learnphp/qt/record_user_ip.htm
如果您只需要IP,您可以创建自己的脚本。只需创建一个类似于 PHP 的后端脚本并从 JS 请求它。示例:http: //php.about.com/od/learnphp/qt/record_user_ip.htm