The Sorting Room, my spam detector, has two parts: a React interface built with Vite and a Flask API that serves the trained models. They run on different addresses while you develop, and that is where one of the most confusing errors in web development appears. The API itself is fine. Open the address in a new tab, or call it with curl , and it answers. Only the browser refuses. Why it happens Browsers protect users with the same origin policy . A page loaded from http://localhost:5173 may not read a response from http://127.0.0.1:5000 , because the port (and here the host) is different. The server has to say "this origin is allowed" by sending an Access Control Allow Origin header. Flask does not send it unless you tell it to. Two things to remember: CORS is enforced by the browser , not by Flask. That is why curl and tools like Postman never show the error. You fix it on the server (or avoid it with a proxy). Changing your React code will not help. Fix 1: allow your front end in Flask Install the extension and give it the origins you trust. Restart Flask after changing it. Avoid origins=" " in production, and never combine with cookies or login credentials. Why a POST sends two requests When React sends JSON, the browser first sends a small OPTIONS request called a preflight to ask "may I send this?". Flask CORS answers it for you. If you hand write your own headers, you must handle OPTIONS too, or the real request never leaves the browser. Fix 2: use the Vite proxy while developing If you would rather not touch the server, let Vite forward the calls so the browser thinks everything is on one origin. Now call fetch("/api/predict") from React. This only works in development, so you still need CORS (Fix 1) or the same domain for production. When it is not really CORS This is the part that costs the most time. If your Flask code crashes and returns a 500 error page, that error response has no CORS header either. The browser reports a CORS error , even though the real bug is an exception in Python. How to tell the difference: 1. Open DevTools, then the Network tab, and click the failed request. 2. Look at the Status . If it says 500 (or the request shows as failed with no status), check the terminal where Flask is running. 3. Read the Python traceback there. Fix that error first, and the CORS message disappears. The same happens when the address is wrong. A typo in the route or a missing http:// also looks like CORS in the console. Checklist Symptom Likely cause What to do Works in curl , fails in the browser CORS not enabled Add flask cors with your origin Preflight ( OPTIONS ) fails Custom headers not allowed Let Flask CORS handle it, or allow the headers CORS error plus a 500 status Python exception Read the Flask terminal Works locally, fails after deploy Production origin missing Add your live site address to origins Remember it as: browser blocks, server allows, and always read the server log before blaming CORS.
Fixing the CORS error between a React app and a Flask API
Your Flask API works in the browser address bar but React says it is blocked by CORS. Here is why it happens, the two-line fix, and the case where CORS is not the real problem.