Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. JavaScript Course

Lesson 15 of 55 · javascript

Fetching Data – `fetch` API & JSON

Duration: 11 mins

The modern fetch API replaces XMLHttpRequest.

fetch('https://api.example.com/users')
  .then(res => {
    if (!res.ok) throw new Error('Network error');
    return res.json();
  })
  .then(users => console.table(users));
  • fetch returns a Promise that resolves with a Response object.
  • Use response.json() to parse JSON; response.text() for raw text.
  • Request options: method, headers, body.

Example: POST JSON data

fetch('/login', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ username, password })
})
.then(r => r.json())
.then(data => console.log(data));
Previous: Asynchronous JavaScript – Callbacks, Promises & async/awaitNext: Error Handling – try/catch, Custom Errors & Debugging