Send data form Flask to Javascript using Ajax request not working

  flask, javascript, python

I try to send the data from Flask to JavaScript using Ajax. I want to alert() retun variable from Flask on a page. In my application are two buttons and after one of them was clicked I want to add comment in alert(). When I click on it this redirect me to blank page only with return results. It can be a problem with my first Ajax request or it doesn’t matter? It return me GET http://127.0.0.1:5000/favicon.ico 404 (NOT FOUND) in console.

Code in JS:

$("#two").on('click',function (e) {
        $.ajax({
            type: "POST",
            url: "/mapaa",
            data: {
                ideevent: $("#ev_n").val(),
            },
            success: function (response) {

                alert(response)

            },

        });
    });

Code in Flask:

@app.route('/mapaa',methods=['GET','POST'])
def mapa():
    user_id = current_user.get_id()
    slownik = {}

    if request.method == "POST":

        if request.is_json:
            req = request.get_json()
                     ...

        elif 'remove-form' in request.form:
            id_event = request.form.get('ev_n')
            logged = current_user
            user_string = str(logged)
            user_n = user_string[6:-1]
            data_participation = db.session.query(Participation)
            id_user = db.session.query(User.id).filter(User.username == user_n).first()
            id_user_to_string = str(id_user)
            id_user_take = id_user_to_string[1:-2]
            id_user_int = int(id_user_take)
            if_exist = db.session.query(Participation).filter(Participation.c.eventId == id_event).filter(Participation.c.userId == id_user_int).count()
            if if_exist==1:
                db.session.query(Participation).filter(Participation.c.eventId == id_event).filter(Participation.c.userId == id_user_int).delete()
                db.session.commit()
                return 'yes'
            else:
                return 'no'

    return render_template('mapaa.html', title='Mapa')

Source: Python Questions

LEAVE A COMMENT