简体   繁体   English

使用 post 方法将数据 reactJS 传递到服务器端(python 烧瓶)

[英]passing data reactJS to server side (python flask) with post method

I am work with react and python.我正在使用 react 和 python。 I want to give data to python server (flask app) from react.我想从反应中将数据提供给 python 服务器(烧瓶应用程序)。

send data with this kind of code:-使用这种代码发送数据:-

  const handledropChange = (value) => {
        console.log(`selected ${value}`);
        fetch('http://127.0.0.1:3002/postdata',{
          method: 'POST',
          headers: {"Content-Type" : "application/json"},
          body: JSON.stringify({'content':value})
        }).then(() => {
          console.log({'content':value})
        })
    };

In python flask app server receve data with this kind of code:在 python flask 应用服务器使用这种代码接收数据:

 @app.route('/postdata' ,methods=['POST'])
 def postdata():
     data = request.json['content']
     conn = get_db_connection()
     cur = conn.cursor()
     cur.execute(f'INSERT INTO data(data) VALUES ({data})')
     cur.close()
     conn.close()
     return data

this is give me error of like:-这是给我这样的错误:-

error in endpoint:- Method Not Allowed The method is not allowed for the requested URL.端点错误:-方法不允许 请求的 URL 不允许该方法。

error in console:- Failed to load resource: the server responded with a status of 400 (BAD REQUEST)控制台错误:-加载资源失败:服务器响应状态为 400(错误请求)

The fetch URL seems incorrect.提取 URL 似乎不正确。 Can you try using:您可以尝试使用:

'http://127.0.0.1:3002/postdata'

instead of代替

'http://127.0.0.1:3002/alldata'

in that fetch() method?在那个fetch()方法中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM