JSON and AJAX in Web development
JSON
JSON stands for JavaScript Object Notation.
When exchanging data between a browser and a server, the data can only be text.
Since the JSON format is a text-based format, it can be easily sent to/from a server.
- JSON is used for Exchanging data
- JSON is a language-independent data format, and can be used by any programming language.
- JSON filenames use the extension
.json
. - JSON used in both Client side and Server side.
JSON Format
people.json
1 | [ |
- JSON objects are surrounded by curly braces
{}
- JSON objects are written in key/value pairs.
- Keys must be strings (surrounded by
""
only), and values must be a valid JSON data type (string, number, object, array, boolean or null). - Keys and values are separated by a colon
:
. - Each key/value pair is separated by a comma
,
.
Client Side - JavaScript & JSON
As we know, JavaScript Objects are different with JSON.
1 | //JS Object |
- Client Sending data: If you have data stored in a JavaScript object, you can convert the object into JSON, and send it to a server.
- Client Receiving data: If you receive data in JSON format, you can convert it into a JavaScript object.
To Convert JS to JSON (for Client Sending Data)
JSON.stringify(jsObject)
To Convert JSON to JS (for Client Receiving data)
JSON.parse(jsonObject)
Server Side: PHP & JSON
PHP has some built-in functions to handle JSON.
-
Encode a value to JSON format by PHP
-
Decode a JSON object into a PHP object or an associative array
-
Server Recieving data: If you receive data in JSON format, you need to decode JSON-format data into a PHP object (by default) or an associative array.
-
Server Sending data: If you send data in PHP format, you need to encode it to JSON-format.
To Convert PHP to JSON (for Server Sending Data)
json_encode($php_object)
1 |
|
To Convert JSON to PHP (for Server Receiving data)
json_decode($json_string)
- return a PHP objectjson_decode($json_string, true)
- return an associative array
1 |
|
AJAX
AJAX known as Asynchoronous JavaScript and XML.
- Asynchoronous means no page refresh required.
XML is a kind of data format, very similar to JSON.
JSON has replaced XML as a more popular data format.
AJAX is a technique for creating fast and dynamic web pages.
- allows web pages to be updated asynchronously by exchanging small amounts of data with the server.
- it is possible to update parts of a web page, without reloading the whole page.
- Classic web pages, which do not use AJAX, must reload the entire page if the content should be changed.
Examples of applications using AJAX:
- Google Search suggestion
- Google Maps
- YouTube
- Facebook tabs
How AJAX Works?
XMLHttpRequest Object
The XMLHttpRequest object can be used to exchange data with a server.
To Create an XMLHttpRequest Object in JS:
var ourRequest = new XMLHttpRequest();
Object Methods:
- Specify the request (request type: GET or POST) :
ourRequest.open('GET', 'https://xxx', true);
- Send the request to server:
ourRequest.send();
Object Properties:
- Returns the response data as a string
ourRequest.responseText;
- Returns the status-number of a request
ourRequest.status;
- 200: “OK”
- 403: “Forbidden”
- 404: “Not Found”