S
S
Spoon in the brain2019-04-13 17:36:31
Flask
Spoon in the brain, 2019-04-13 17:36:31

How to display information that came from the front-end in Flask's back-end?

Good afternoon, I created an Ajax request to the server with information. So, this information comes, but I need to accept it on the server and, let's say, output it somewhere, how is it done in Flask? Maybe there will be examples?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-04-13
@vessels

First, you should check what is being formed in $data, there is most likely heresy. And in general it is not clear why such body movements, if you can simply:

$('#log-form').submit(function(event) {
    event.preventDefault();
    $.post($(this).attr('action'), $(this).serialize(), function(result) {
        // Обрабатываете ответ от бэкенда как вам угодно
    });
});

Secondly
from flask import jsonify
from flask import request

@app.route('/', methods=['POST'])
def form_handler():
      if request.form.get('login') == 'admin':
          if request.form.get('pass') == 'qwerty':
              return jsonify({'type': 'success', 'msg': 'Привет, Админ!'})
      return jsonify({'type': 'error', 'msg': 'Неправильное имя пользователя или пароль'})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question