why fetch from react to flask back end not working

//There are some packages you can use like fetch-with-proxy or node-https-proxy-agent, fetch-with-proxy can get HTTP_PROXY env var to set proxy, and you can use the other create proxy agent.

//But I recommend that to use other package to send request like axios.

import axios from 'axios';
const axios_ = axios.create({
  baseURL: 'http://localhost:5000',
  headers: { 'Content-Type': 'application/json' },
})

class App extends React.Component  {
  componentDidMount() {
    axios_.get('/')
      .then(response => {
        console.log(response.text()) //Here is the text() i said before
        this.setState({ snippets: response.data })
      })
      .catch(error => {
        console.log(error)
      });
  }
...}

4
8
Jacek11 85 points

                                    Latest update: Sorry about I misunderstood about proxy in nodejs, and fetch() can support this kind of proxy.

//After I do some trial and error, you just need to change you path '/' to '/something_else' and the proxy work well.

//Wrong information:

//This is not issue about python and flask.

//You use fetch() in javascript, and this is native js function, so it is not related with proxy you set in package.json.

//If you want to use fetch() to get other port, you have to give it the whole url http://localhost:5000/ or you will get the same port of you react app.

fetch('http://localhost:5000/')
      .then(response => {
        console.log(response.text()) //Here is the text() i said before
        this.setState({ snippets: response.data })
      })
      .catch(error => {
        console.log(error)
      });

4 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
why fetch from react to flask back end not working fetching data from flask with react to fetch data from reactjs to flask api flask react fontend flask api with react frontend flask react requests run flask and react together react and flask api react js python flask build app python flask react tutorial flask backend with react connnect react with python flask Reactjs and Flask react front end flask back end python flask and react tutorial post json python flask and react tutorial react flask integration flask return react frontend react flask app simple flask react app frontend flask project react front end flask backend building a full app react and flask backend building a full app react and flask react with flask python flask and react running react script without jsx on flask react python backend flask and react how to connect react to flask form routes for flask react app react app with flask backend flask react app flask js project flask + react flask with react react app with flask tutorial how to incorporate react js files to flask how to use react with flask GET call from react to flask hook GET call from react to flask how to implement react js with Flask connect react to flask backend flask react how to use useEffect on flask api integrating react with flask react and flask how to render react with flask flask server react build python flask reactjs tutorial flask and react native how to connect react to flask? flask fron end Sessions with a React/Redux Frontend and Flask API Backend creating flask api with react front end flask and react tutorial for windos development flask react rest api working with react and flask python making api request and render them in react simple
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source