Reputation: 141
I'm trying to get the client's IP address. This is my code so far. Any suggestions?
<script type="application/javascript">
function getIP(json) {
document.write("My public IP address is: ", json.ip);
}
</script>
<script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=getIP"></script>
Upvotes: 14
Views: 81480
Reputation: 2278
Just in case someone gets here, it is not possible anymore to get ip address through WebRTC to prevent vulnerabilities as mentioned here:
https://stackoverflow.com/a/26850789/2795615
Best idea is to use a external service to get the ip, although they can have some limitations, there is a very nice updated comparison here:
https://stackoverflow.com/a/35123097/2795615
Upvotes: 0
Reputation: 71
<script>
$.getJSON('http://ip-api.com/json', function(ipData){
document.write(ipData.query)
});
</script>
Upvotes: 0
Reputation: 2493
How to get current ip address using angular? Please refer link : https://github.com/apilayer/freegeoip#readme
OR
getIpAddress(){
this.http.get<{ip:string}>('https://jsonip.com')
.subscribe( data => {
console.log('th data', data);
this.ipAddress = data
})
}
Upvotes: 0
Reputation: 2493
Try This.Little bit Help to you.
<script type="application/javascript">
function getIP(json) {
document.write("My public IP address is: ", json.ip);
}
</script>
<script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=getIP"></script>
Upvotes: 11
Reputation: 88
try this
$.ajax({
url: "//api.ipify.org/?format=json",
dataType: 'JSON',
}).success(function(data) {
console.log(data.ip);
}).error(function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
});
Upvotes: 0
Reputation: 2576
function user_location() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log( this.responseText);
}
};
xhttp.open("GET", "//api.ipify.org?format=json", true);
xhttp.send();
}
Upvotes: 3
Reputation: 1292
The JSON variable that is returned can be navigated like any object in javascript.
<textarea id="text" style="width:100%;height:120px;"></textarea>
<script type="application/javascript">
function getIP(json) {
document.getElementById("text").value = JSON.stringify(json, null, 2);
for (key in json) {
document.write("<br>" + key + " : ", json[key]);
}
}
</script>
<script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=getIP"></script>
Upvotes: 0
Reputation: 783
please try below code:-
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
Ip Address:=<h3 class='ipAdd'><h3>
</body>
<script>
$(document).ready(function ubsrt()
{
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new RTCPeerConnection({iceServers:[]}),
noop = function(){};
pc.createDataChannel("");
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(ice){
if(!ice || !ice.candidate || !ice.candidate.candidate) return;
var myIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
$('.ipAdd').text(myIP);
pc.onicecandidate = noop;
};
});
</script>
</html>
Upvotes: 5