Science Library - free educational site

JSON and AJAX

AJAX

Asynchronous JavaScript and XML: AJAX is a JavaScript technique for making HTTP requests without reloading a page.

AJAX is not a programme, like PHP or Javascript, but a formalised scripting procedure for enabling information on a webpage to be updated without the need to reload the page. This allows for faster and smoother operations, and less user inconvenience.

One data format that makes AJAX more effective is JSON.

JSON

JSON, or JavaScript Object Notation, is an open format for transferring data. It derives from the JavaScript language and is a popular alternative to XML because it's lightweight and easily human-readable.

Here's what it might look like:

  • {
  • "name": "John",
  • "surname": "Smith",
  • "age": 42,
  • "address": "Dent Street, Gallifrey",
  • "postal code": "6900",
  • "tags": ["time", "traveller"]
  • }

As you can see, JSON is based on a series of name-value pairs, and if you know JavaScript, you will no doubt see how similar the notation is.

Using JSON with AJAX

One common application for JSON on the internet is as a data format for AJAX (ironically, Asynchronous Javascript And Xml).

Suppose we already have a function AJAX that uses the XMLHttp object to make an AJAX request to the specified page sending any data and then calling a callback function, passing it the server's response.

If the response text is encoded in JSON, we can simply make it into an object using the eval function:

AJAX({"url": 'example.php', "data": 'params=value', function(r) {
var responseObj = eval(r.text);
}});

The eval function evaluates the string as code, and, because JSON is valid JavaScript syntax, will return an object with the data from the server.

Because it is so popular, many other programming languages have native functions to encode and decode JSON. PHP, for instance, has the json_encode and json_decode functions, which will return a string with the JSON equivalent of a passed object, and vice versa.

! Beware !

Be careful when applying the eval function to a string of unknown or untrusted origin. Hackers may try to inject malicious code into your page, and this is a massive security breach. I personally never use this function unless I wrote the script myself.

Sean Bone

ZumGuy Network webmaster

Visit my Internet forum: ZumGuy Internet Forum

Per una versione di quest'articolo in Italiano vedi: JSON

Translation by Transalpine Technical Translations.

Content © Renewable-Media.com. All rights reserved. Created : February 2, 2014 Last updated :February 14, 2016

Latest Item on Science Library:

The most recent article is:

Trigonometry

View this item in the topic:

Vectors and Trigonometry

and many more articles in the subject:

Subject of the Week

Mathematics

Mathematics is the most important tool of science. The quest to understand the world and the universe using mathematics is as old as civilisation, and has led to the science and technology of today. Learn about the techniques and history of mathematics on ScienceLibrary.info.

Mathematics

Great Scientists

Charles Goodyear

1800 - 1860

Charles Goodyear is a model of the determined inventor, who struggled to find the secret to a better rubber, despite years of financial hardship and frustration.

Goodyear
Lugano English

Quote of the day...

Nonno's explanations always did a round-robin circuit of science - history - philosophy, then back to science. Always back to science. As if it were that that drove the mechanism of time, and not the other way round. And philosophy? Well, that just went along for the ride.

ZumGuy Internet Promotions

IT information forum by Sean Bone