I have a website developed in react js on the front end. The front makes a request (POST method) to a fastapi python back end.
On the web interface, when I click on the sent button which calls the fastapi API, there is the following error:

on my local computer, everything is working fine. But when I do the production on an o2switch server, the above bug comes back.
For react js, I use axios to do the POST request:
Axios.post("http://localhost:8001/calcul", {
localisation: "MA",
critere_priviligier: "12",
largeur_lame: "12", espace_lame: "12"
)
.then(res => {
console.log("1111", res.data);
console.log(res)
navigate('/response', {state: {localisation: res.data.autonomie_luminere,
coefficient_cm: res.data.coefficient_cm,
consommation_anuelle: res.data.consommation_anuelle,
information: res.data.information,
optimise: res.data.optimise}});
}).
then(function(json) {
console.log("JSON: ", json);
})
.catch(error => console.error(`Error, ${error}`));
On fastapi, I took into account the CORS:
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from calcul import Calcul, CoefficientCM
import pandas as pd
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
For prod, I use build folder:
npm run build
And for fastapi I use uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8001 --reload
As I mentioned before, everything works fine locally. But it's when I put in production on an o2switch server that there is this bug.
Fastapi and react js are in the same server
localhostrefers to your own computer (i.e. the computer where the browser is running - that's probably not where you've hosted the API in production).localhostrefers to the localhost for your browser. Not the same host as the frontend code is delivered from.localhost?