简体   繁体   English

后期方法烧瓶和angularJS

[英]post method flask and angularJS

i'm new at using flask. 我是使用烧瓶的新手。 I've been trying to do a function to handle a post request, the problem is that: in the angular controller it always goes to error or if it's not an error i can't acces to the data sent in the corresponding python function. 我一直在尝试做一个函数来处理发布请求,问题是:在角度控制器中,它总是会出错,或者如果不是错误,我将无法访问在相应的python函数中发送的数据。

This is what i have: 这就是我所拥有的:

AngularJS controller: AngularJS控制器:

$http({
        method  : 'POST',
        url     : 'http://localhost:5000/holapost',
        headers: { 'Content-Type': 'application/json' },
        data: JSON.stringify({sent: $scope.TextToSend)
    })
      .success(function(data) {
           irrelevant....
        })
      .error(function(data) {
            irrelevant....
        });
  };

python flask 蟒蛇烧瓶

def post_1():
  received=request.data
  .....
  return jsonify({'msj': "what was sent was received well"})

Is something wrong? 有什么事吗 Any help appreciated, thanks! 任何帮助表示赞赏,谢谢!

I don't have much experience with AngularJS, but after looking through the docs at https://docs.angularjs.org/api/ng/service/ $http, you're http call looks fine. 我没有使用AngularJS的丰富经验,但是浏览了https://docs.angularjs.org/api/ng/service/ $ http上的文档后,您的http调用看起来不错。

As for Flask, make sure you are routing post_1() through @app.route('/holapost') . 至于Flask,请确保通过@app.route('/holapost')路由post_1() @app.route('/holapost') Regarding not being able to access the data from the post request, the flask docs recommend using the form attribute: request.form . 关于无法访问发布请求中的数据,flask文档建议使用form属性: request.form ( http://flask.pocoo.org/docs/0.10/quickstart/#the-request-object ) http://flask.pocoo.org/docs/0.10/quickstart/#the-request-object

It would also be helpful if you ran your flask app with the debugger using: app.run(debug=True) . 如果使用以下程序通过调试器运行烧瓶应用程序,这也将有所帮助: app.run(debug=True) I hope this helps! 我希望这有帮助!

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

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