Chuyển đến nội dung

Bài 33 · Debugging Workflows

Debugging Network and CORS Failures

Trace failed requests step by step: read status codes and the Headers tab, tell CORS from network errors, understand preflights and fix missing headers.

"Failed to fetch." "Blocked by CORS policy." "net::ERR_CONNECTION_REFUSED." Browsers report network problems with short, unfriendly messages, and the Console alone rarely tells you what to fix. The Network panel has the whole story: the request, the response, the headers, the timing and the reason a request was blocked.

This lesson gives you a workflow to classify a failure in under a minute, and explains CORS well enough to fix it on the server where it belongs.

Step 1: find the request

Open the Network panel before triggering the action. Tick Preserve log so a page navigation does not clear the list, and use the Fetch/XHR filter to hide images and scripts. Failed requests are drawn in red. If a request never appears, it never left the browser: a script error happened first, a content blocker (an extension or CSP) stopped it, or the URL was wrong in code.

Click the request and check, in order:

  1. Headers > General: Request URL, Method, Status Code. A status of (failed) or (canceled) with an error such as net::ERR_NAME_NOT_RESOLVED means no HTTP response was received at all.
  2. Headers > Response Headers and Request Headers: content type, cookies, authorization, CORS headers.
  3. Payload (what you sent) and Response or Preview (what came back; the server often sends a JSON error message you can read).
  4. Timing: where the time went, and Initiator: which line of code made the request.

Classifying the failure

What you see Meaning Look at
(failed) net::ERR_NAME_NOT_RESOLVED DNS failed Typo in host, DNS, offline
net::ERR_CONNECTION_REFUSED Nothing listens on that host and port Server not started, wrong port
net::ERR_CERT_AUTHORITY_INVALID Bad TLS certificate Local development certificates
(blocked:mixed-content) HTTP request from an HTTPS page Use HTTPS URLs
Status 400 Bad request Payload and Content-Type
401 / 403 Not authenticated / not allowed Authorization header, cookies
404 Not found URL, route, base path
429 Rate limited Retry-After header
500 / 502 / 503 / 504 Server or gateway error Server logs, response body
blocked by CORS policy in the Console The server responded, but without permission headers Response headers

Note the key difference: a CORS failure is not a server failure. The server usually executed your request successfully; the browser refused to give the response to your script.

CORS in five sentences

Browsers enforce the same-origin policy: a script on https://app.example may not read responses from https://api.example unless that server opts in. The server opts in with the header Access-Control-Allow-Origin, naming the allowed origin (or * for public data without credentials). For "non-simple" requests (JSON bodies with Content-Type: application/json, custom headers like Authorization, methods like PUT or DELETE), the browser first sends a preflight: an automatic OPTIONS request asking what is allowed. Only if the preflight reply allows the origin, method and headers does the real request follow. If you send cookies (credentials: "include"), the server must also send Access-Control-Allow-Credentials: true and a specific origin, never *.

In the Network panel, you see the preflight as a separate OPTIONS row (you may need to turn off the "Hide preflight" style filter or look for the row with the method OPTIONS) just before the real request.

A CORS decision simulator

The stand-in below implements the core of the browser's decision for a request, so you can experiment with different servers and see which message DevTools would print.

JS Stand-in: simulate a CORS decision

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Case 1 shows a server with no CORS headers at all. Case 2 passes the origin check but fails on the method and headers in the preflight. Case 3 is a complete, correct configuration. This is the debugging order you follow in DevTools: preflight response first, then the actual response.

Fixes, in the right place

CORS is fixed on the server (or a proxy), never in front-end code: add the headers listed above for the exact origin, answer OPTIONS with a 204 status, and include Vary: Origin if the allowed origin varies. In development, a dev-server proxy (so that the browser sees one origin) avoids the issue. Using mode: "no-cors" in fetch does not fix it; it returns an unreadable "opaque" response. Browser extensions that disable CORS only hide the problem on your machine.

More useful tools in the panel

  • Right-click a request > Copy > Copy as cURL (or fetch) to replay it from a terminal; if it works in cURL, the server is fine and the issue is browser policy.
  • Block request URL and Block request domain simulate a failing CDN or third party.
  • Request blocking and Throttling > Offline test error handling in your UI.
  • Override content (Sources > Overrides) lets you change a response locally and test your app against it.
  • The Issues tab (bottom drawer) lists cookie, CORS, mixed content and other problems with explanations and links.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. A CORS error means:

    CORS is a browser rule about reading responses cross-origin; the server often did process the request.
  2. 2. What is a preflight request?

    Browsers send OPTIONS before non-simple cross-origin requests.
  3. 3. A request uses credentials: 'include'. Which Access-Control-Allow-Origin value is invalid?

    With credentials the server must name the exact origin; a wildcard is rejected.
  4. 4. A request shows status (failed) net::ERR_CONNECTION_REFUSED. What should you check first?

    That error means nothing accepted the TCP connection, so no HTTP response (and no headers) exist yet.

Summary

  • Keep Preserve log on, filter Fetch/XHR, and check Headers, Payload, Response, Timing and Initiator.
  • Classify: no response (DNS, refused, TLS), HTTP error status, or CORS block.
  • CORS is enforced by the browser and fixed with server headers; watch the OPTIONS preflight.
  • Copy as cURL, request blocking, overrides and the Issues tab speed up diagnosis.

Next: Project: Audit a Slow Page

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.