Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!