2015-06-29 33 views
3

請幫助我解決問題:表單提交後,Flask不想重定向。 代碼:表格提交後,重定向不工作吧jquery

from flask import Flask, render_template, request, redirect, url_for 
.... 
@app.route('/auth/') 
def auth(): 
return render_template('auth.html') 

此代碼工作以及:),auth.html呈現形式:

{% extends "system.html" %} 
{% block content %} 
<form id="authen"> 
<input type="text" id="avtor_sku" pattern="[0-9]{1,13}" maxlength="13" value='' autofocus required> 
</form> 
{% endblock %} 

提交此表 - 在JS代碼:

$(document).ready(function(){$('#authen').submit(function(){ var avtor_sku = $("#avtor_sku").val(); 
data1= '' + avtor_sku; 
$.ajax({type: "GET", url: "/auth_echo/", contentType: "application/json; charset=utf-8", 
data: {auth_echo_value: data1}, success: function() {alert(1)}}); 
return false;});});  

警報(1)在這個代碼中 - 工作。 路線/ auth_echo /:

@app.route('/auth_echo/', methods=['GET']) 
def auth_echo(): 
return redirect(url_for('openday')) 

重定向無法正常工作。爲什麼?

回答

4

您不能在AJAX之後重定向。

你可以做這樣的事情,如果你想重定向:

$.ajax({ 
      type: "POST", 
      url: "http://example.com", 
      dataType: 'json', 
      success: function(data){ 
       window.location.href = data; 
      } 
     }); 

注意,是假設數據是一個新的URL,但你可以做window.location.href=new_url到「重定向」