Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda