Load JSON with fetch()

The modern way to do AJAX. Check response.ok before using the data.

AJAX · Load JSON with fetch()Beginner
async function loadUsers() {
  try {
    const res = await fetch('api/users.php');
    if (!res.ok) throw new Error('HTTP ' + res.status);
    const users = await res.json();
    console.log(users);
  } catch (err) {
    console.error('Could not load users:', err);
  }
}
loadUsers();
fetchjsonasync