I'm trying to get the client's IP address. This is my code so far. Any suggestions?
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>
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>
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();
}
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>
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);
});
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
})
}
<script>
$.getJSON('http://ip-api.com/json', function(ipData){
document.write(ipData.query)
});
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.