使用fetch()时HTTP Headers未发送问题求助
Hey there! Let's figure out why your Authorization header isn't reaching the server—especially since Postman works perfectly. Here are the most common culprits and fixes:
1. CORS Preflight Request Issues
Browsers send an OPTIONS preflight request before cross-domain POST/PUT/DELETE requests (or any request with custom headers like Authorization). If your server doesn't properly handle this preflight, the browser might block the actual request from sending the Authorization header.
Check & Fix:
- Verify that your server returns the
Access-Control-Allow-Headersheader in its preflight response, and that it explicitly includesAuthorization. For example:Access-Control-Allow-Headers: Content-Type, Authorization - Make sure the server also returns
Access-Control-Allow-Originset to your frontend's origin (use*only for testing, avoid in production).
2. Frontend Code Accidentally Overwriting Headers
It’s easy for a line of code to overwrite or remove your Authorization header without you noticing, especially if you’re using request libraries or interceptors.
Check & Fix:
- If using Axios, inspect request interceptors to ensure they aren’t replacing headers:
// Double-check interceptors don't erase your Authorization header axios.interceptors.request.use(config => { // Use spread syntax to preserve existing headers config.headers = { ...config.headers, 'X-Custom-Header': 'value' }; return config; }); - For vanilla
fetch, confirm you’re merging headers correctly instead of overwriting:const options = { method: 'POST', headers: { ...yourPreconfiguredHeaders, // Keep existing headers intact 'Authorization': 'Bearer your-token-here' }, body: JSON.stringify(yourData) };
3. Browser Extensions Blocking the Header
Privacy-focused extensions (like uBlock Origin, Privacy Badger) sometimes strip Authorization headers as part of anti-tracking measures.
Check & Fix:
- Test your request in an incognito window with all extensions disabled. If it works there, add your frontend domain to the extension’s allowlist.
4. Typos or Case Sensitivity
Even small mistakes can break things. Double-check the header name:
- It’s
Authorization(note the capitalization: A followed by "uthorization"). Avoid misspellings likeAuthorisation(British English) unless your server explicitly expects it.
5. Verify the Actual Request in DevTools
Don’t just trust that the header is attached to your options variable—confirm what’s actually sent:
- Open your browser’s DevTools → Network tab.
- Trigger the request, click on it, and check the "Request Headers" section.
- If the Authorization header isn’t listed here, your code isn’t actually attaching it (even if you think it is).
If none of these fix the issue, share a redacted snippet of your request code and we can dig deeper!
内容的提问来源于stack exchange,提问作者Ketan Malhotra




