【问题标题】:how to redirect to another page in python pyramid via ajax when success成功时如何通过ajax重定向到python金字塔中的另一个页面
【发布时间】:2016-09-17 20:05:47
【问题描述】:

如果代码根据 ajax 成功返回的值匹配,我想将用户重定向到另一条路线。 它现在工作得很好我如何编写脚本来重定向到下一个路由,比如 php 中的重定向。

AJAX 来检查代码匹配并且它的工作很好。接下来是什么

<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>

这是视图配置

@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'

【问题讨论】:

标签: jquery python ajax redirect pyramid


【解决方案1】:

我不喜欢 Python,但您似乎正在向 Ajax 调用返回“成功”或“错误”。

在您的 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;
        }
    }
}

【讨论】:

  • 我试图使用 jquery 加载而不是忘记了这个存在。工作
猜你喜欢
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 2011-02-26
  • 1970-01-01
  • 2022-01-27
  • 2022-11-15
  • 1970-01-01
  • 2012-12-29
相关资源
最近更新 更多