-3

I'm trying to convert json data from a website and display it in html:

var obj = 'http://ip-api.com/json';
document.getElementById('info').innerHTML = obj.country + ", " + obj.as;

I am trying to get the JSON code from a url called http://ip-api.com/json, and I am trying to get the data to be read and then shown into html, can anyone help?

1
  • This is probably the most basic thing that any JS program ever does. There are about 101 ways to approach it, all of them covered in hundreds and thousands of intros, tutorials, and blog posts. Commented Oct 13, 2017 at 13:27

1 Answer 1

-1

You will need to perform an Ajax call to fetch the remote the json. I suggest you to use jQuery library for this.

Before </body>:

<script type="text/javascript" src="//code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
  jQuery.getJSON("http://ip-api.com/json", function(obj) { 
    jQuery("#info").html(obj.country + ", " + obj.as); 
  });
</script>
Sign up to request clarification or add additional context in comments.

11 Comments

The information on the website didnt display on the html doc?
Are you running this html in your local machine? If so, replace src="//code.jquery.com/jquery-1.12.4.min.js" by src="http://code.jquery.com/jquery-1.12.4.min.js"
I am running it off a local machine and what am I doing wrong because the JSON data wont display on the html <html> <body> <script type="text/javascript" src="code.jquery.com/jquery-1.12.4.min.js"></script> <script type="text/javascript"> jQuery.getJSON("ip-api.com/json", function(obj) { jQuery("#info").html(obj.country + ", " + obj.as); }); </script> </body> </html>
And where is #info HTML container??
I'm clearly not to experienced with html, but I get the concept and I am working on it, but I don't really understand what you mean by HTML container
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.