I'm building a flask application and I want a python function to send back either a string or a template to my javascript code. Unfortunately, javascript can't seem to decode the objects my function is sending back despite using the jsonify and render_template functions. Below is my code: Python
@app.route('/rep_bot',methods=['GET','POST'])
def bot_rep_py():
print("bot_rep_py lancée")
if request.method=='POST':
input_text=request.form["js_input"]
print(input_text)
output_list=get_response(input_text)
print(output_list)
if type(output_list) == list:
return render_template('form_comp.html',data=map(json.dumps,output_list))
else:
print("entrée dans else")
return jsonify('0') #si le métier n'est pas connu on renvoie 0 et on transmet le msg coté js
else:
return "erreur pas POST"
JavaScript
function bot_rep_js(input) {
console.log("bot_rep_js lancée")
console.log(input)
$.post("http://127.0.0.1:5000/rep_bot",
{js_input:input},
function(data){return data;}
);
console.log("bot_rep_js finie")
}
And the console:
clickUpdate lancée
script.js:66 premier click
script.js:45 bot_rep_js lancée
script.js:46 devops
script.js:51 bot_rep_js finie
script.js:68 undefined
EDIT: problem came from the fact that the flask server didn't get enough time to solve my request so assigning the variables immediately made them "undefined"