首页 > 解决方案 > 没有从烧瓶路由到 ReactJS 前端的响应

问题描述

我正在将表单数据从 ReactJS 前端传递到烧瓶后端以解决预测问题,并希望以 JSON 格式获得预测,但我无法从后端收到任何响应。

反应前端代码:

handleFormSubmit = (event) => {
  fetch('http://127.0.0.1:5000/predict', {
    method: 'POST',
    body: JSON.stringify(this.state.patientData)
  }).then(response => response.json())
    .then(response => {
      this.setState({
        result: response.result
      })
    })
}   

烧瓶后端代码:

@app.route('/predict', methods=['POST'])
def handleJSON():
    data = request.get_json(force = True)
    test_data = list(data.values())
    test_data = pd.DataFrame([test_data])

    # Column Transformer   
    ct_pkl = open('ct.pkl', 'rb')
    ct = pickle.load(ct_pkl)
    test_data = ct.transform(test_data)

    # Logistic Regression Model
    lr_pkl = open('lr.pkl', 'rb')
    model = pickle.load(lr_pkl)
    pred = model.predict(test_data)

    response = jsonify({"statusCode": 200, 'result': pred[0]})
    return response

标签: jsonreactjsflask

解决方案


推荐阅读