Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
Why my flask app is not redirecting after AJAX request?

I am making a web app to help disabled people to navigate the internet. It is taking voice command through a JavaScript and sending the command to the Python app.py Flask app. However, weirdly enough it is not redirecting in any way and giving me 500 internal server error.

This is the JavaScript function which sends command -

// This function sends command to python
function sendCommand(command){
    let userCommand = {
        "command": command
    }

    $.ajax({
        type: "POST",
        url: "/command",
        data: JSON.stringify(userCommand),
        contentType: "application/JSON",
        dataType: 'json',
        success: function(){
            window.location.href = "temp.html";
        }        
    })
}

And this is the python flask app -

# Importing required libraries and functions
from flask import Flask, render_template, request, redirect
import speech_recognition as sr
import sys

# Initiating Flask
app = Flask(__name__)

# Command Global variable
COMMAND = ""

# Route to Command (Index) page
@app.route("/", methods=["GET", "POST"])
def index():
    return render_template("index.html")

# Processing Command
@app.route('/command', methods=["POST"])
def get_javascript_data():
    if request.method == "POST":
        JSONdict = request.get_json()
        COMMAND = JSONdict["command"]
        print(f'{COMMAND}', file=sys.stdout)

        if "search" in COMMAND:
            print("TODOSearch")
        elif ("music" and "play") in COMMAND:
            print("TODO")
        else:
            print("TODO")
            
        return redirect("/redirect")

@app.route("/redirect")
def redirect():
    return render_template("redirect.html")

What is my fault over here?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You won't be able to redirect from flask on a POST request. Instead use return 200

Then the success function in your Ajax request should trigger.

If you need more flexibility, you can also return json data to the "success" or "error" function. return json.dumps({'success' : True}), 200, {'ContentType' : 'application/json'}

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda