Для моего одностраничного веб-приложения мне нужно:
- Отправить json из .js в колбу (ГОТОВО)
- Запустите ввод через функцию Python - getString () и получите вывод str (DONE)
- Отправьте вывод str обратно в файл .js (ПРОБЛЕМА)
Вот приложение фляги:
@app.route('/',methods =['GET','POST'])
def index():
req = json.dumps(request.get_json())
if request.method == 'POST':
result = getString(req) #Function outputs a string
return jsonify(result)
else:
print('Not Received')
return render_template('index.html')
if __name__ == '__main__':
app.run()
Проблема в том, что jsonify (результат) не отправляется, вероятно, из-за request.method == 'POST'переключение на другое, когда вызывается jsonify. Есть ли способ исправить мой код для отправки вывода str в .js?
Вот .js:
//To send info to flask
document.querySelector('#generate').addEventListener('click',function() {
var json_inputs = JSON.stringify(inputs);
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "/",
traditional: "true",
data: json_inputs,
dataType: "json"
});
})
//To receive from Flask
$.ajax({
url: "/",
type: 'GET',
success: function(data) {
console.log(data);
}
});