Troubleshooting CORS Errors in Web Applications
CORS errors are among the most confusing web development issues. Learn how Cross-Origin Resource Sharing works, why browsers block requests, and how to fix common misconfigurations.
Key Takeaways
- Cross-Origin Resource Sharing is a browser security mechanism that blocks web pages from making requests to domains different from the one serving the page.
- The server must respond with `Access-Control-Allow-Origin`, `Access-Control-Allow-Methods`, and `Access-Control-Allow-Headers` headers that match the request.
- "The value of the Access-Control-Allow-Origin header must not be the wildcard '*' when credentials mode is 'include'" — you can't use `*` with cookies; specify the exact origin.
Hash Generator
Generate SHA-1, SHA-256, SHA-384, SHA-512 hashes from text
Understanding CORS
Cross-Origin Resource Sharing is a browser security mechanism that blocks web pages from making requests to domains different from the one serving the page. When your frontend at app.example.com calls an API at api.example.com, the browser sends a CORS preflight request to check permissions.
How Preflight Works
For "non-simple" requests (those with custom headers, non-standard methods, or certain content types), the browser sends an OPTIONS request before the actual request. The server must respond with Access-Control-Allow-Origin, Access-Control-Allow-Methods, and Access-Control-Allow-Headers headers that match the request.
Common Error Patterns
"Access to XMLHttpRequest has been blocked by CORS policy" — the server is not sending the required CORS headers. "The value of the Access-Control-Allow-Origin header must not be the wildcard '*' when credentials mode is 'include'" — you can't use * with cookies; specify the exact origin. "Method PUT is not allowed" — the server's Access-Control-Allow-Methods header doesn't include the HTTP method you're using.
Server-Side Configuration
Return Access-Control-Allow-Origin with the specific requesting origin (not * if cookies are involved). Include all required methods in Access-Control-Allow-Methods. List all custom headers in Access-Control-Allow-Headers. Set Access-Control-Max-Age to cache preflight responses and reduce roundtrips.
Debugging Steps
Use browser DevTools Network tab to inspect both the OPTIONS preflight and the actual request. Check response headers on the preflight — missing or incorrect headers here cause the browser to block the subsequent request. Test with curl to verify the API works outside the browser context (curl doesn't enforce CORS).
เครื่องมือที่เกี่ยวข้อง
รูปแบบที่เกี่ยวข้อง
คู่มือที่เกี่ยวข้อง
JSON vs YAML vs TOML: Choosing a Configuration Format
Configuration files are the backbone of modern applications. JSON, YAML, and TOML each offer different trade-offs between readability, complexity, and tooling support that affect your development workflow.
How to Format and Validate JSON Data
Malformed JSON causes silent failures in APIs and configuration files. Learn how to format, validate, and debug JSON documents to prevent integration errors and improve readability.
Base64 Encoding: How It Works and When to Use It
Base64 converts binary data into ASCII text, making it safe for transmission through text-based systems. Learn when Base64 is the right choice and when alternatives like hex encoding or URL encoding are more appropriate.
Best Practices for Working with Unix Timestamps
Unix timestamps provide a language-agnostic way to represent points in time, but they come with pitfalls around time zones, precision, and the 2038 problem. This guide covers best practices for storing and converting timestamps.
Troubleshooting JWT Token Issues
JSON Web Tokens are widely used for authentication but can be frustrating to debug. This guide covers common JWT problems including expiration errors, signature mismatches, and payload decoding issues.