Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

example to write json data by jquery with html text

Tags:

json

html

jquery

I am beginner in programming, I want someone to give me an example to show me how to write JSON data with HTML text

<!DOCTYPE html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    </head>
    <body>
        <h1>antar<h2>
        <script>
            $.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {
                document.open();    
                document.write('Latitude: ' + data.latitude + '\nLongitude: ' + data.longitude + '\nCountry: ' + data.address.country);
                document.close();
            });
        </script>
    </body>
</html>
like image 371
Europeangoldfinch Avatar asked Dec 31 '12 10:12

Europeangoldfinch


Video Answer


1 Answers

You can add in your html something like:

 <h2 id='lat'></h2>
    <h2 id='long'></h2>
    <h2 id='country'></h2>

and then refactor your script as:

$(document).ready(function(){

$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {

  $("#lat").text(data.latitude);  
  $("#long").text(data.longitude);  
  $("#country").text(data.country);      

});
 });

​Here a working example you can play with.

like image 101
Felice Pollano Avatar answered Oct 08 '22 18:10

Felice Pollano