While trying to fetch
a simple api made using FastAPI with Reactjs in the frontend, I keep getting no response:
Here's the FastAPI code:
@app.get("/haha")
async def read_haha():
return "haha"
Postman's test on the api :
And lastly reactjs code :
import logo from './logo.svg';
import './App.css';
import React, { useEffect, useState } from 'react';
import axios from 'axios';
import 'bootstrap/dist/css/bootstrap.min.css'
import Button from 'react-bootstrap/Button';
function App() {
const [response, setResponse] = useState('');
useEffect(() => {
fetch('http://127.0.0.1:8000/haha')
.then(res => res.text())
.then(data => {
console.log(data); // add this line to see the data returned by the API
setResponse(data);
})
.catch(error => console.error(error));
}, []);
return (
<div>
<h1>API Response:</h1>
<p>{response}</p>
</div>
);
}
export default App;
The result of all of this :
Since I am new to FastAPI and React maybe the issue is obvious to everyone, but in my case I spent hours without finding a solution to it...