[Python] Why does Flask redirect to "GET /? HTTP/1.1"

Discussão em 'Python' iniciado por Stack, Agosto 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm writing a simple Flask application in which I want to send some data from the front-end to the Flask app, have it perform some operations, and return new data to the front-end for display. I have made similar applications before, and by returning the POST response object, instead of render_template(), I'm able to simply return the data and do what I want with it on the front-end. However, this time I'm having problems.

    I make a POST request from the Jquery in the front-end. Everything seems to work fine, I can see the data being returned in the browser console, except the page reloads before I can display the new data. It reloads to http://xxx.x.x.x:5000/?.

    I can see the get request for /? in the Flask console. I'd like to know why it is doing this, and how I can get it to stop.

    (I've found this difficult to research because most search engines will silently ignore any question marks in a query.)

    Flask app:

    import json
    from flask import Flask, Response, render_template, request
    from src.simple_lookup import analogy_lookup

    app = Flask(__name__)
    app.config['DEBUG'] = True

    @app.route('/')
    def hello_world():
    return render_template('index.html', results=['a', 'b'])

    @app.route('/get_words_simple', methods=['POST'])
    def get_words_simple():
    print('request.form:', request.form.keys)
    data = analogy_lookup(request.form['keyword'], request.form['base'], request.form['goal'])
    resp = Response(json.dumps(data), status=200, mimetype='application/json')
    print('data:', data)
    print('resp:', resp)
    return resp

    if __name__ == "__main__":
    app.run()


    Jquery:

    $.post('get_words_simple?', data, function(json, status) {
    console.log('response:', json);
    if (json.hasOwnProperty('error')) {
    $('.results').append('<p>' + json.error);
    return;
    }

    var words = json.words;
    $.each(words, function(i, text) {
    var p = $("<p>");
    p.append(text);
    $('.results').append(p);
    });
    });


    Flask console:

    127.0.0.1 - - [27/Dec/2018 11:12:21] "GET / HTTP/1.1" 200 -
    127.0.0.1 - - [27/Dec/2018 11:12:21] "GET /static/js/main.js HTTP/1.1" 200 -
    request.form: <bound method MultiDict.keys of ImmutableMultiDict([('keyword', ''), ('base', ''), ('goal', '')])>
    127.0.0.1 - - [27/Dec/2018 11:12:23] "GET /? HTTP/1.1" 200 -
    127.0.0.1 - - [27/Dec/2018 11:12:23] "GET /static/js/main.js HTTP/1.1" 200 -
    data: ['obtuvo', 'takata', 'stadshypotek', 'kriwet', 'shafiee', 'martorell', 'collum', '29,400', 'muteesa', 'patzek']
    resp: <Response 111 bytes [200 OK]>
    127.0.0.1 - - [27/Dec/2018 11:12:23] "POST /get_words_simple? HTTP/1.1" 200 -

    Continue reading...

Compartilhe esta Página