简体   繁体   English

成功时如何通过Ajax重定向到python pyramid中的另一个页面

[英]how to redirect to another page in python pyramid via ajax when success

I want to redirect user to another route if the code matches depending upon the values returned in the ajax success. 如果代码根据ajax成功返回的值匹配,我想将用户重定向到另一条路由。 Its working great now How do I write the script to redirect to the next route like redirect in php. 现在工作正常,我该如何编写脚本以重定向到下一个路由,例如php中的redirect。

AJAX to check the code matches and its working great. AJAX检查代码匹配及其工作情况。 What next 接下来是什么

<script>
$("#codeModal form").submit(function(event) {
        event.preventDefault();
        var codetyped=$(this).find(".codeinput").val();
        $.ajax({
            type:"post",
            url:"{{request.route_url('testingajax')}}",
            data:{'codetyped':codetyped},
            success:function(res){
                alert(res);
            }
        });
        });
</script>

Here is the view config 这是视图配置

@view_config(route_name='testingajax', renderer='json')
def hello(request):
    # return request.session['secretcode']
    # return request.params.get('codetyped')
    if int(request.params.get('codetyped')) == request.session['secretcode']:
        return 'Success'
    else:
        return 'Error'

I'm not into Python, but it seems you are returning either 'Success' or 'Error' to your Ajax call. 我不喜欢Python,但似乎您在向Ajax调用返回“成功”或“错误”。

Within your Ajax success callback function, you could simply check the returned value and redirect from there. 在Ajax成功回调函数中,您可以简单地检查返回的值并从那里重定向。

success:function(res){
    // Test if there is a returned value in the res variable
    if(typeof res !== 'undefined'){

        // Check the res variable from a few options
        switch(res){
            case 'Success':
                window.location.href='/some-success-page.html';
            break;
            case 'Error':
                window.location.href='/some-error-page.html';
            break;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM