🌐CORS error (cross-site request blocked)
Access to fetch at 'https://api.example.com' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
For safety, the browser blocked a request going to a server at a different address. It's not so much that your code is wrong; the other server hasn't allowed your site.
🤔 Why does it happen?
- The browser (frontend) called an outside API directly, and that API doesn't allow your address
- The backend server you built has no CORS setting that allows it
- Even a small difference in http/https or the port number counts as a "different site"
🔧 Try this
- Don't call outside APIs straight from the browser; route them through your own server (API route). This is the surest fix
- If it's your own server, add your site's address to its allow list (CORS)
- If the request uses an API key, it has to be called from the server anyway so the key isn't exposed
🤖 Ask your AI like this
I get the CORS error below in the browser console. Please change it so the outside API isn't called directly from the browser, but goes through our server (API route) instead. Make sure the API key is only used on the server. [Paste the full error here]
Replace the [ ] parts with your own situation, and paste the error with a few extra lines before and after it.
Related terms
Other errors in this group
- Page not found (404)
404 Not Found - Internal server error (500)
500 Internal Server Error - Authentication failed (401)
401 Unauthorized - No permission (403)
403 Forbidden - Too many requests (429)
429 Too Many Requests - Couldn't send the request (Failed to fetch)
TypeError: Failed to fetch - Connection refused (localhost refused to connect)
ERR_CONNECTION_REFUSED - Mixed content (http request on an https page)
Mixed Content - Request method not allowed (405)
405 Method Not Allowed - What you sent is too big (413)
413 Payload Too Large - The middle server got no answer (502)
502 Bad Gateway - Service temporarily unavailable (503)
503 Service Unavailable - Redirects loop forever (too many redirects)
ERR_TOO_MANY_REDIRECTS - Blocked by an ad blocker
net::ERR_BLOCKED_BY_CLIENT - An image or file won't load
Failed to load resource: 404 (Not Found)