具有ajax响应的Flask不会重定向

时间:2016-12-26 18:15:12

标签: javascript jquery python ajax

当我从烧瓶视图功能将URL发回我的javascript文件时,不会发生重定向,而是返回 select max(veturattable.id) as id, max(veturattable.vetura) as vetura, max(veturattable.modeli) as modeli, max(veturattable.ngjyra) as ngjyra, max(veturattable.targa) as targa, max(renttable.pagesa) as pagesa, sum(hargjimettable.shuma) as shuma from veturattable left join hargjimettable on hargjimettable.veturaid= veturattable.id left join renttable on renttable.veturaid = veturattable.id group by veturattable.id;

我的观点功能:

Type error: 'NoneType' object is not subscriptable

我的Ajax请求:

@app.route('/login', methods=['POST','GET'])
def login():
    if request.method == "POST":
        print('check')
        values = request.json
        print(values)
        result = db.session.query(Emp_Details).filter_by(ID=values['ID']).first()
        print(result.Password)
        if result.Password == values['Password'] and result.Department == 'HR':
                print(jsonify(url=url_for('hr_department')))
                return jsonify(url=url_for('hr_department'))
        elif result.Password == values['Password'] and result.Department == 'DEVELOPMENT':
                return url_for('devs')
        else:
            abort(401)
    else:

        return render_template("login.html")

我的控制台中的日志:

$.ajax({
                    type :"POST",
                    url :"/login",
                    contentType: 'application/json;charset=UTF-8',
                    data : JSON.stringify(data, null, '\t'),
                    dataType : 'json',
                    success : function (data) {
                        window.location.href = data['url']
                    },
                    error : function (response) {
                        alert(response)

                    },


                });

2 个答案:

答案 0 :(得分:0)

您发送的data似乎有问题:

如果我使用

,这对我有用
data = {'ID': 'foobar'};

HTML:

<script>

    data = {'ID': 'foobar'};

    $.ajax({
        type: "POST",
        url: "/test/ajax",
        contentType: 'application/json;charset=UTF-8',
        data: JSON.stringify(data, null, '\t'),
        dataType: 'json',
        success: function(data) {
            window.location.href = data['url']
        },
        error: function(response) {
            alert(response)
        },
    });

</script>  

烧瓶中:

@app.route('/test/ajax', methods=['GET', 'POST'])
def test_ajax():

    if request.method == "POST":
        data = request.json
        print('data:', data)

        result = {'url': url_for('index')}
        print('result:', result)

        return jsonify(result)

    else:
        return render_template('test-ajax.html')

答案 1 :(得分:0)

我可能会迟到回复这个,但是,对于那些来到这里并使用 Fetch API 的人来说,@furas 的回复对我从来没有用过。

但是下面的这个解决方案有效

浏览器端的响应如下

 Response{type: "basic", url: "http://127.0.0.1:5000/billing/print_receipt/20", redirected: true, status: 200, ok: true}

和 Javascript 重定向将像这样完成

if(response.ok & response.redirected){
           location.replace(response.url
    }