Web Programming Languages

Web Coding in Ajax with JSON

A single-page application has all the HTML code in one file, and does not reload other pages. Instead, the application uses Ajax to download HTML code fragments or data fragments to display as the user navigates from page to page.

The idea behind Ajax is that the page doesn't have to reload, so it is faster. Also, lots of boilerplate like headers, footers, and sidebar don't have to be re-loaded all the time.

This JSON implementation employs minimal communication between the client and the server. Instead of transferring the page content DIV, it transfers only the data that actually changes from page to page. This includes:

  • The data in the three tables on the Table pages.
  • The image links and captions on the Directory page.
  • The contents of the table on the File Read and Database Read pages.
  • The existing information on the various forms for editing.
  • The requested source code.

Compare full data to JSON data

Sending the data as JSON instead of full HTML results in much less data transfer. Here are some examples, and links to actual webservice data.

Ajax full HTMLSizeJSON data onlySizeSavings
Multiplication Table2200 Multiplication Table55075%
States Table4600 States Table220052%
Images2550 Images60075%
File data2000 File data55072%
MySQL data3000 MySQL data67078%

Note that the tables with high information to markup ratio (for example, the states) do not have as great a reduction in size, because most of the information in the full HTML version is the raw data anyway.

There are tradeoffs: the client must have JavaScript code that assembles the JSON data into the final HTML. This code makes the HTML and JavaScript files larger than they would have to be otherwise, and executing the JavaScript can cost extra time.

JSON: Dynamic Table Data

Multiplcation Table

Choose a table: MultiplicationASCIIUS States

Choose a sort: AbbreviationStatePopulationRankCapital

Show PHP source code »

JSON: Directory Read

Read the contents of a directory of images. Exclude the non-image directory entries. Create a caption by deleting the file extension and capitalizing the file name.

Choose a directory: ComicsEmbroideryCarvingTech

We can change the order of the files (for example, birth order instead of alphabetical order) by prepending each file name with a number and removing the number from the caption string.

Images courtesy of:
Stone Paperweight at World of Judaica
Embroidery Designs at 4-Hobby.com
Dumbing of Age
Scary Go Round
Questionable Content

Show PHP source code »

JSON: Display File Data Contents

Write to this file »

Show PHP source code »

JSON: Edit the data file

Edit a course
College:
Department:
Course #:
Course name:
FieldOld dataNew data
College
Department
Course #
Course name

Show the contents of this file »

Show PHP source code »

JSON: Form Handling and Write to File

Add a course
College:
Department:
Course #:
Course name:

The following data was added to the data file:

College
Department
Course #
Course name

Show the contents of this file »

Show PHP source code »

JSON: Display MySQL Database Contents

Write to this database table »

Show PHP source code »

JSON: Edit the MySQL Database

Edit a park
Park:
City:
State: (4 characters max)
Latitude:
Longitude:
FieldOld dataNew data
Park
City
State
Latitude
Longitude

Show the contents of this database table »

Show PHP source code »

JSON: Write to MySQL Database

Add a park
Park:
City:
State: (4 characters max)
Latitude:
Longitude:

The following data was added to the data file:

Park
City
State
Latitude
Longitude

Show the contents of this database table »

Show PHP source code »