Fetch details from JSON string return by URL using JavaScript

Last Reply 3 months ago By dharmendr

Posted 3 months ago

http://api.ipinfodb.com/v3/ip-city/?key=3b8fbb8b954ae5daad70021283f3fd56b&ip=933.207.145.193&format=json

How can i fetch detail from above URL as mentioned in below code, i need to get dynamic details.

<!DOCTYPE html>
<html>
<body>

<h2>Convert a string into a function.</h2>

<p id="demo"></p>
</body>
</html>
<script>

var text = '{ "name":"John", "age":"function() {return 30;}", "city":"New York"}';
var obj = JSON.parse(text);
obj.age = eval("(" + obj.age + ")");

document.getElementById("demo").innerHTML = obj.name + ", " + obj.age(); 

</script>

 

Posted 3 months ago Modified on 3 months ago

Hi fahimahmed,

Check this example. Now please take its reference and correct your code.

HTML

<input type="button" id="demo" value="Parse URL" onclick="ParseURL()" />
<script type="text/javascript">
    function httpGet(theUrl) {
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", theUrl, false);
        xmlHttp.send(null);
        return xmlHttp.responseText;
    }
    function ParseURL() {
        var json = httpGet("http://api.ipinfodb.com/v3/ip-city/?key=3b8fbb8b954ae5daad70021283f3fd56b&ip=933.207.145.193&format=json");
        var response = eval('(' + json + ')');
        alert("IP Address: " + response.ipAddress + "\nStatus Message: " + response.statusMessage);
    }
</script>

Screenshot