Suppose you need to create a single HTML page to display data, but the data comes from another website source. This HTML example will show you how to access the data using jQuery then present it on your single-page web app.

In this example, I still display the names of famous classical composers on my SPA by dynamically loading JSON from a different website.

Model

Our model will consist of a JSON file that contains a single key "author".

[
{ "author": "Wolfgang Amadeus Mozart" }, 
{ "author": "Johann Sebastian Bach" }, 
{ "author": "Ludwig van Beethoven" }, 
{ "author": "Johannes Brahms" }, 
{ "author": "Antonín Dvořák" }, 
{ "author": "Isaac Albéniz" }, 
{ "author": "Tomaso Albinoni" }, 
{ "author": "Alessandro Scarlatti" }, 
{ "author": "Samuel Barber" }, 
{ "author": "Agustín Barrios" }, 
{ "author": "Béla Bartók" }, 
{ "author": "Hector Berlioz" }, 
{ "author": "Georges Bizet" }, 
{ "author": "Alexander Borodin" }, 
{ "author": "Max Bruch" }, 
{ "author": "Anton Bruckner" }, 
{ "author": "Ferruccio Busoni" }, 
{ "author": "Frédéric Chopin" }, 
{ "author": "Aaron Copland" }, 
{ "author": "Arcangelo Corelli" }, 
{ "author": "Claude Debussy" }, 
{ "author": "Domenico Scarlatti" }, 
{ "author": "Paul Dukas" }, 
{ "author": "Gabriel Fauré" }, 
{ "author": "John Field" }, 
{ "author": "George Gershwin" }, 
{ "author": "Philip Glass" }, 
{ "author": "Enrique Granados" }, 
{ "author": "Edvard Grieg" }, 
{ "author": "George Frideric Handel" }, 
{ "author": "Joseph Haydn" }, 
{ "author": "Leoš Janáček" }, 
{ "author": "Johann Christian Bach" }, 
{ "author": "Aram Khachaturian" }, 
{ "author": "Zoltán Kodály" }, 
{ "author": "Erich Wolfgang Korngold" }, 
{ "author": "Morten Lauridsen" }, 
{ "author": "Franz Liszt" }, 
{ "author": "Gustav Mahler" }, 
{ "author": "Felix Mendelssohn" }, 
{ "author": "Niccolò Paganini" }, 
{ "author": "Arvo Pärt" }, 
{ "author": "Francis Poulenc" }, 
{ "author": "Sergei Prokofiev" }, 
{ "author": "Giacomo Puccini" }, 
{ "author": "Henry Purcell" }, 
{ "author": "Sergei Rachmaninoff" }, 
{ "author": "Maurice Ravel" }, 
{ "author": "Erik Satie" }, 
{ "author": "Franz Schubert" }, 
{ "author": "Robert Schumann" }, 
{ "author": "Alexander Scriabin" }, 
{ "author": "Jean Sibelius" }, 
{ "author": "Johann Strauss II" }, 
{ "author": "Igor Stravinsky" }, 
{ "author": "Pyotr Ilyich Tchaikovsky" }, 
{ "author": "Antonio Vivaldi" }, 
{ "author": "Richard Wagner" }, 
{ "author": "Eric Whitacre" }, 
{ "author": "John Williams" }, 
{ "author": "Nikolai Rimsky-Korsakov" }, 
{ "author": "Gioachino Rossini" }, 
{ "author": "Camille Saint-Saëns" }, 
{ "author": "Dmitri Shostakovich" }, 
{ "author": "Bedřich Smetana" }, 
{ "author": "Georg Philipp Telemann" }, 
{ "author": "Ralph Vaughan Williams" }, 
{ "author": "Giuseppe Verdi" }, 
{ "author": "Franz von Suppé" }, 
{ "author": "Edward Elgar" }, 
{ "author": "Carl Philipp Emanuel Bach" }, 
{ "author": "Giovanni Pierluigi da Palestrina" }, 
{ "author": "Claudio Monteverdi" }, 
{ "author": "Modest Mussorgsky" }, 
{ "author": "Josquin des Prez" }, 
{ "author": "Jean-Baptiste Lully" }, 
{ "author": "Jean-Philippe Rameau" }, 
{ "author": "Charles Ives" }, 
{ "author": "Paul Hindemith" }, 
{ "author": "Olivier Messiaen" }, 
{ "author": "William Byrd" }, 
{ "author": "Hildegard of Bingen" }, 
{ "author": "François Couperin" }, 
{ "author": "Benjamin Britten" }, 
{ "author": "César Franck" }, 
{ "author": "Gustav Holst" }, 
{ "author": "Anton Webern" }, 
{ "author": "Dieterich Buxtehude" }, 
{ "author": "Gaetano Donizetti" }, 
{ "author": "Thomas Tallis" }, 
{ "author": "Ottorino Respighi" }, 
{ "author": "Guillaume de Machaut" }, 
{ "author": "Mikhail Glinka" }, 
{ "author": "Alban Berg" }, 
{ "author": "Steve Reich" }, 
{ "author": "Vincenzo Bellini" }, 
{ "author": "Arnold Schoenberg" }, 
{ "author": "Muzio Clementi" }, 
{ "author": "John Dowland" }, 
{ "author": "John Cage" }, 
{ "author": "Pérotin" }, 
{ "author": "Heitor Villa-Lobos" }, 
{ "author": "Carlo Gesualdo" }, 
{ "author": "Michael Praetorius" }, 
{ "author": "Johann Jakob Froberger" }, 
{ "author": "Barbara Strozzi" }, 
{ "author": "Johann Pachelbel" }, 
{ "author": "Wilhelm Friedemann Bach" }, 
{ "author": "Carl Ditters von Dittersdorf" }, 
{ "author": "Antonio Salieri" }, 
{ "author": "Johann Nepomuk Hummel" }, 
{ "author": "Carl Czerny" }, 
{ "author": "Fanny Mendelssohn" }, 
{ "author": "Luigi Cherubini" }, 
{ "author": "Giacomo Meyerbeer" }, 
{ "author": "Johann Strauss I" }, 
{ "author": "Fernando Sor" }, 
{ "author": "Mauro Giuliani" }, 
{ "author": "Louis Spohr" }, 
{ "author": "Carl Maria von Weber" }, 
{ "author": "Luigi Cherubini" }, 
{ "author": "Luigi Boccherini" }, 
{ "author": "Johann Stamitz" }, 
{ "author": "Carl Stamitz" }, 
{ "author": "Leopold Mozart" }, 
{ "author": "Michael Haydn" }, 
{ "author": "Antonio Caldara" }, 
{ "author": "Johann Adolph Hasse" }, 
{ "author": "Girolamo Frescobaldi" }, 
{ "author": "Heinrich Schütz" }, 
{ "author": "Francesco Cavalli" }, 
{ "author": "Thomas Campion" }, 
{ "author": "Philippe de Vitry" }, 
{ "author": "John Dunstaple" }, 
{ "author": "Giovanni Gabrieli" }, 
{ "author": "Domenico Gabrielli" }, 
{ "author": "Charles Gounod" }, 
{ "author": "Jules Massenet" }, 
{ "author": "Guillaume Dufay" }, 
{ "author": "Christoph Willibald Gluck"}
]
https://codepen.io/chrisaiv/pen/NNWvBO.js

Single-page App

This HTML file dynamically loads jQuery, Pure.css, and the Model from CodePen (see above). Once the JSON loads, I run a loop that populates the text data into an HTML table.

<html>
<head>
	<title></title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
        
<link rel="stylesheet" href="https://yui.yahooapis.com/pure/0.6.0/pure-min.css">
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
	<script type="text/javascript">
	$(document).ready(function(){
		var RAW_JSON = "https://codepen.io/chrisaiv/pen/NNWvBO.js";
		function getData( url ){
		    $.ajax({
		        url: url,
		        dataType: "json",
		        encoding:"UTF-8",
		        success: ajaxSuccess,
		        error: ajaxError,
		    });
		}
 
		function ajaxError( xhrInstance, message, optional ){
		    console.log( "ajaxError:" );
		}
 
		function ajaxSuccess( data, status ){
			//Write your Parse XML Function
		    parseData( data );
		}
 
		function parseData( data ){
			//console.log(data)
			$(data).each(function(n, value){
				//Data
				var author = value.author
				var url    = value.url
				//Structure
				var tr     = $("<tr>")
				var tbody  = $("tbody")
				
					var a  = $("<a>").attr( { href: url, alt: author, target: "_blank" } ).text(author)
					var td = $("<td>").append(a)
					
				//Since we want 2 x <td>'s per <tr> we need to write a special condition
				if(n % 2 === 0){
					tr.append(td)
					tbody.append(tr)
				} else {
					var lastTR = $("tr:last-child")
					lastTR.append(td)
				}
			});
			$( "tr:odd" ).addClass("pure-table-odd")
		}
		getData(RAW_JSON)
	});
	</script>

</head>
<body>
	<section>
		<div class="pure-u-1">
			
			<h4>Names of Famous Composers:</h4>
			<div id="credits">
				<table class="pure-table">
					<tbody>
						<!--
							Javascript Generated from JSON
						-->
					</tbody>
				</table>
			</div>

</div>
	</section>	
</body>
</html>
https://codepen.io/chrisaiv/pen/NNWvBO

Live Example

This is the actual HTML working live.

Names of Famous Composers:


Live Example #2

Click here to see a second example.