How to send POST data to flask using Reactjs fetch How to send POST data to flask using Reactjs fetch reactjs reactjs

How to send POST data to flask using Reactjs fetch


It is a problem of Cross Origin Resource Sharing. In you case you are trying to access the API endpoint from the different URL as the API is being served on itself. By the way, 127.0.0.1:3000 and 127.0.0.1:5000 considered as two different URLs, therefore causing the error that you are referring to. So, to fix this you need to perform the following steps:

  1. Install Flask-CORS:

    pip install flask-cors
  2. Include this code in your Flask application (probably __init__.py):

    from flask_cors import CORSCORS(app)

That's it!