title: In a flask application, how can I redirect to a new page during ajax post based on condition discovered in view?
body: <p>In my flask application I have a sales page that collects information and saves it to the database via an ajax post request. There is a limited number of consoles available for users to record sales. A user can takeover a console from another user, but under this condition I want the initial user to lose access to the console. The console data retrieved in the view of the sales page has the information needed to determine this (available after each new sale).</p>
  
  <p>I know I can’t do this with a standard “return redirect(url_for(‘dup_console’))” in the view because I’m posting through an ajax call.</p>
  
  <p>I’ve spent a lot of time searching through SO and have found solutions that suggest performing the conditional logic in the success portion of the ajax call</p>
  
  <p>Example: </p>
  
  <pre><code>success: function(data) {
      if (data.redirect) {
          window.location.href = data.redirect;
      }
  }
  </code></pre>
  
  <p>I’m not sure if this will work in my case as I can’t figure out how to get the console data from the view to the success routine. When I check the response information from the success routine it contains the html from the page.</p>
  
  <p>Is there a way to get the information I need from the view to the success routine of the ajax post?</p>
  
  <p>Here is a portion of my code (I've tried to reduce it to just the relevant pieces):</p>
  
  <p><strong>View - sales_console</strong></p>
  
  <pre><code>@main.route('/sales_console/&lt;int:id&gt;', methods=['GET', 'POST'])
  @login_required
  def sales_console(id):
      product_data = get_product_data()
      console_data = get_console_data(id)
      if request.method == 'POST':
          returned_data = json.dumps(request.get_json())
          returned_data_dict = ast.literal_eval(returned_data)
  
          &lt;review returned data and store in database&gt;
  
          &lt;retrieved data indicates if console id used by other device&gt;
          if duplicate_console:
              redirect to page indicating console loss (I know I can’t do it like this)
  
  return render_template('sales_console.html', product_data=product_data, console_data=console_data)
  </code></pre>
  
  <p><strong>Java Script routine available to sales_console.html template</strong></p>
  
  <pre><code>function record_sale(event,track_sales_data,console_number)
  {
      var sales_data_json = JSON.stringify(track_sales_data);
      var url = "/sales_console/"+console_number;
      $.ajax({
          url: url,
          type : "POST",
          data: sales_data_json,
          contentType:"application/json",
          success: function(response) {
              console.log("success!!!");
              console.log(response);
          },
          error: function(error) {
              console.log("failure!!!");
              console.log(error);
          }
      });
  };
  </code></pre>