简体   繁体   中英

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.

This is what i have:

AngularJS controller:

$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.

As for Flask, make sure you are routing post_1() through @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 . ( 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) . I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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