H
H
Hlib2019-06-14 21:38:58
Flask
Hlib, 2019-06-14 21:38:58

Why does it give a 404 error when requesting data?

I'm requesting data from the server, but it gives a 404 error, what's the problem?
Flask:

@app.route('/test', methods=['POST'])
def testing():
    test = ['hi']
    return jsonify(test)

Getting data on page load:
componentDidMount(){
    this.props.getTest('/test')
  }

The dispatch argument in the connection:
getTest: url =>{
      dispatch(test(url))
    }

Action:
export function testSuccsess(testFiles){
  return {
    type: 'PERSONS_FETCH_DATA_SUCCESS',
    testFiles
  }
}

export function test(url){
  return (dispatch)=>{
    fetch(url)
      .then(response =>{
        if(!response.ok){
          console.log(response)
          throw new Error(response.statusText)
        }
        return response;
      })
      .then(response=> response.json())
      .then(testFiles => dispatch(testSuccsess(testFiles)))
  }
}

The reducer that is thrown into the combine reducers:
export default function testing(state = [], action){
  switch (action.type) {
    case 'PERSONS_FETCH_DATA_SUCCESS':
      return action
      break;
    default:
      return state;
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pcdesign, 2019-06-14
@Mysianio

As far as I understand, you created a route in Flask with the POST method
. And you send the request in React with the usual GET, which is why you get 404.
https://stackoverflow.com/questions/38510640/how-t...

A
Andrey Suha, 2019-06-14
@andreysuha

Can an action be asynchronous? Also for your when the server and front should be running on the same domain and the same port

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question