Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ReactJS、Superagent和Python(flask)发布_Javascript_Python_Flask_Reactjs_Superagent - Fatal编程技术网

Javascript 使用ReactJS、Superagent和Python(flask)发布

Javascript 使用ReactJS、Superagent和Python(flask)发布,javascript,python,flask,reactjs,superagent,Javascript,Python,Flask,Reactjs,Superagent,我试图将一些数据发布到我用Flask制作的Python后端。我在一个React组件中使用。由于某种原因,我一直收到HTTP错误400 我读过很多关于使用JQuery和flask的类似问题的帖子。这里的解决方案是以与我相同的方式设置contentType,并对数据进行JSON.stringify。我尝试过stringify,但它没有改变任何事情。仍在获取HTTP 400 有什么想法吗 JS代码: request.post(link) .set('Content-Type', 'applicati

我试图将一些数据发布到我用Flask制作的Python后端。我在一个React组件中使用。由于某种原因,我一直收到HTTP错误400

我读过很多关于使用JQuery和flask的类似问题的帖子。这里的解决方案是以与我相同的方式设置contentType,并对数据进行JSON.stringify。我尝试过stringify,但它没有改变任何事情。仍在获取HTTP 400

有什么想法吗

JS代码:

request.post(link)
 .set('Content-Type', 'application/json; charset=utf-8')
 .send({tag: 'tag1', comment: 'Cool'})
 .end(function(err, res){
    console.log(res);
 });
Python函数/端点:

@app.route('/api/leavecomments', methods=['POST'])
def comment_to_photos():
  comment = request.form['comment']
  print(comment)
  tag = request.form['tag']
...

因此,对于存在此问题的任何其他人来说,他们需要使用名为get_json的方法,该方法将以json格式向其传递值。在上面的代码中,它将这些值作为查询字符串post参数进行查找,post参数通常通过表单post发送。对于AJAX JSON post,数据存在于request.body中

有关更多信息,请查看


superagent版本:1.6.1烧瓶版本:0.10.1请参阅此文档哇谢谢!我确实完全关注JavaScript部分,但事实证明问题出在Python方面。使用get_json确实解决了我的问题!如果你把答案贴在下面,我会把它标记为解决我问题的答案。谢谢!好的,谢谢,我做到了:)