JavaScript - Ajax & JSON
Overview
Estimated time: 10–15 minutes
Ajax allows you to fetch data asynchronously without reloading the page. JSON is the standard format for data exchange.
Learning Objectives
- Make Ajax requests using
fetch
orXMLHttpRequest
. - Parse and generate JSON data.
Prerequisites
Ajax with fetch
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data));
JSON Example
const obj = { foo: 1 };
const json = JSON.stringify(obj);
const parsed = JSON.parse(json);
Common Pitfalls
- Always handle errors in Ajax requests.
Summary
Ajax and JSON are essential for modern, dynamic web applications.