Skip to content

Fetch Network Error

Quick summary

Your fetch request is failing with a TypeError — the server is unreachable, the URL is wrong, or CORS is blocking the request.

Why this happens

  • The server is down or unreachable

  • Wrong URL or protocol (http vs https)

  • CORS policy blocks the request

  • Network connectivity issues

Minimal example

✗ Broken code
try {
  const res = await fetch("http://api.example.com/data");
} catch (err) {
  // TypeError: Failed to fetch
  // No error handling — app crashes
}
✓ Fixed code
try {
  const res = await fetch("https://api.example.com/data");
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  const data = await res.json();
} catch (err) {
  console.error("Request failed:", err.message);
  // Show user-friendly error message
}

Fix the URL (https), check response.ok for HTTP errors, and handle both network and HTTP errors in the catch block.

How to diagnose

  • Check the browser console for the error message

  • Verify the URL is correct and the server is running

  • Check the Network tab for failed requests

  • Look for CORS errors in the console

How to fix

  • Correct the URL and ensure the server is running

  • Fix CORS configuration on the server

  • Add error handling with try/catch for network failures

  • Use a proxy server to avoid CORS issues in development

How to prevent

  • Always handle fetch errors with try/catch or .catch()

  • Configure CORS properly during development

  • Use environment variables for API URLs to avoid typos

Related Resources

Related Glossary

Related Lessons

← Back to language