如何使用jQuery获取客户端IP地址 [英] How to get client IP address using jQuery

查看:436
本文介绍了如何使用jQuery获取客户端IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆