title: How to redirect with Flask and jQuery
body: <p>I'm using Flask as my backend and jQuery for a personal project I'm working on.</p>
  
  <p>To login I want to do this:</p>
  
  <pre><code>$.ajax({ 
      type: "POST",
      data: JSON.stringify(body), //username and password
      contentType: 'application/json; charset=utf-8',
      url: "/login",
      success: successFunction,
      error: errorFunction,
      complete: completeFunction
  });
  </code></pre>
  
  <p>In the errorFuction I would tell the user that their username or password is incorrect etc.</p>
  
  <p>On the backend my /login route looks like this</p>
  
  <pre><code>@app.route("/login", methods=['GET', 'POST'])
  def login():
      if(request.method == "POST"):
          #retrieve the username and password sent
          data = request.json
  
          if(data is None or not 'username' in data or not 'password' in data):
              abort(400)
          else:
              count = User.query.filter(User.username == data['username']).count()
              if(count == 0):
                  abort(404) #that user doesnt exist
              else:
                  passIsCorrect = User.query.filter(User.username == data['username'],
                                                    User.password == data['password']).count()
                  if(passIsCorrect):
                      session['user'] = data['username']
                      return redirect(url_for('index'))
                  else:
                      abort(401)
  
      else:
          return render_template('login.html')
  </code></pre>
  
  <p>However on the client side, the browser doesn't redirect and if I look in the response object in the complete function I see what would normally be return from my '/' route: 200 OK and the index.html template.</p>
  
  <p>My question is:</p>
  
  <p>Is there some way I can intercept make the client redirect?</p>
  
  <p>I assume the issue is because jquery is initiating the request and not the browser.</p>
  
  <p>My first attempt at solving this problem was to construct the response myself using <code>make_response</code> and set the Location header but this resulted in the same behaviour. My current solution is to return 200 and then the client does <code>window.location = "/"</code>, but this seems hacky</p>