sequenceDiagram participant Client participant Server Client->>Server: Initial Request Server->>Client: HTML File Client->>Server: Subsequent Request Server->>Client: HTML File
AJAX, JSON and RESTful APIs
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
April 8, 2025
Two main participants:
They communicate over HTTP: the client issues a request, and the server returns a response, forming the standard request/response cycle.
<head>
<link rel="stylesheet" href="https://server1.com/style.css">
</head>
<body>
<img src="https://server3.com/image.png">
<script src="https://server2.com/script.js"></script>
</body>
server1.com
, server2.com
, and server3.com
.Disadvantages:
sequenceDiagram participant Client participant Server Client->>Server: Initial Request Server->>Client: HTML File Client->>Server: Subsequent Request Server->>Client: HTML File
The relationship between the server and client in the server-side rendering approach
sequenceDiagram participant Client participant Server Client->>Server: Initial Request Server->>Client: HTML File Client->>Server: AJAX Call Server->>Client: JSON Payload
The relationship between the server in the AJAX approach
content-type
, content-length
keep-alive
, upgrade-insecure-requests
accept
, accept-encoding
, accept-language
, host
and user-agent
content-type
, content-encoding
, last-modified
connection
, date
, keep-alive
, transfer-encoding
access-control-allow-origin
, etag
, server
, set-cookie
, vary
, x-frame-options
X-Frame-Options
prevents framingFeature-Policy
can disable camera/microphone, etc.The most common status codes are:
(CRUD) are the four basic operations or actions of persistent storage.
REST stands for Representational State Transfer
Architectural style for designing web APIs
Coined by Roy Fielding in his 2000 PhD dissertation
Resources
Server Response
let person = {
name: "Philip J. Fry", // string
age: 23, // number
"weight": 172.5, // number
friends: ["Farnsworth", "Hermes", "Zoidberg"], // array
};
person.age;
person["weight"]; // 172.5
person.friends[2]; // "Zoidberg"
let propertyName = "name";
console.log(person[propertyName]); // "Philip J. Fry"
function()
{}
or comments like // this is a comment.JSON.parse( /* JSON string */ )
– converts JSON string into Javascript objectJSON.stringify( /* Javascript Object */ )
– converts a Javascript object into JSON textfetch
takes a URL string as a parameter to request data (e.g. pokemon information in JSON format) that we can work with in our JS file.async function populateInfo() {
const URL = "https://pokeapi.co/api/v2/pokemon/ditto";
await fetch(URL) // returns a Promise!
...
}
fetch
call returns a Promise
object which will help us with this uncertainty.fetch()
fetch()
function returns a PromisestatusCheck()
.json()
and .text()
.json()
:
.text()
:
const BASE_URL = "some_base_url.com"; // you may have more than one
async function makeRequest() {
let url = BASE_URL + "?query0=value0"; // some requests require parameters
try {
let result = await fetch(url)
await statusCheck(result)
// result = await result.text(); // use this if your data comes in text
// result = await result.json(); // or this if your data comes in JSON
processData(result)
} catch(err) {
handleError(err); // define a user-friendly error-message function
}
}
function processData(responseData) {
// Do something with your responseData! (build DOM, display messages, etc.)
}
async function statusCheck(res) {
if (!res.ok) {
throw new Error(await res.text());
}
return res;
}
Backend development and Introduction to Node.js
Web Programming