Reputation: 8277
This is my first attempt at interfacing a Flask backend with an Angular frontend. I am trying to get the frontend to collect JSON data generated on the backend. This is what the code looks like.
Flask:
@app.route('/weather/loc', methods=["POST","GET"])
def say_hi():
location = request.form.get("location", default="London")
try:
location, condition, temp = weatherAtLocation( location)
content = "Hello there %s, your weather is %s with a temperature of %s°C." %(location, condition.lower(), temp)
return_dic = {
"location":location,
"condition":condition.lower(),
"temperature": temp
}
return jsonify(return_dic)
except:
return jsonify( {})
This seems to work fine after manual testing. My problem is in the Angular, the function making the call is:
hasclicked(){
this.HttpClient.post("http://127.0.0.1:5000/weather/loc",
{
"location":"London"
})
}
My problem is that I am unfamiliar with TypeScript and could not find an example showing me how to store the returned JSON data.
Upvotes: 0
Views: 325
Reputation: 1368
You need to subscribe to the post()
method or else the call will never be executed. You will then be able to interact with the response. See here for more information regarding making POST requests.
Upvotes: 1