search

Home  >  Q&A  >  body text

python - flask uses ajax for background login interaction. After verifying the password, how to relocate to index.html?

flask uses ajax for background login interaction. After verifying the password, it will not redirect to the index.
ajax code is as follows:

 $.ajax({
                url:'/login',
                type:'post',
                data:{
                    user:$('#user').val(),
                    password:$('#password').val(),
                },
                beforeSend:function(){
                    $.messager.progress({
                        text:'正在登陆准备中......',
                    });
                },
                success:function(data,response,status){
                    $.messager.progress('close');
                    if (data>0){
                        location.href='/templates/index.html';
                    }else{
                        $.messager.alert('登陆失败!','用户名或密码错误!','warning',function(){
                            $('#password').select();
                        });
                    }
                }
            });

After filling in the account and password on the login interface, the post goes to login for processing. The login code is as follows:

def login():
    user1=request.values.get('user')
    password=request.values.get('password')
    if user1:
        mydb=mysql.connector.Connect(database='dh_inf_manage',user='root',password='password')
        mycur=mydb.cursor(dictionary=True)
        query_emp='select * from dic_user where emp_sn=%s'
        mycur.execute(query_emp%user1)
        emp_result=mycur.fetchone()
        if emp_result is not None:
            emp_sn=emp_result['emp_sn']
            emp_name=emp_result['name']
            hashpw=emp_result['password']
            emp_dept=emp_result['emp_dept_sn']
            if check_password_hash(hashpw,password):
                session['emp_dept'] = emp_dept
                user=User()
                login_user(user)
                redirect(url_for('index'))
                return '1'
            else:
                return '0'
        else:
            return '0'
    return render_template('login.html')

index code:

@app.route('/',methods=['GET', 'POST'])
@login_required
def index():
    return render_template('index.html')
某草草某草草2749 days ago629

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-05-18 10:53:10

    location.href='/';

    reply
    0
  • Cancelreply