使用URL_for()创建python flask动态URL

使用URL_for()创建python flask动态URL,python,ajax,flask,Python,Ajax,Flask,我有一个ajax js函数: if (data.result == "Successfully joined"){ window.location.href='{{ url_for( 'chat', name = "test" ) }}'; } python中的和函数: @app.route('/chat/<string:name>') def chat(name): return render_template("chat.html", c=name, s="Ja

我有一个ajax js函数:

if (data.result == "Successfully joined"){
    window.location.href='{{ url_for( 'chat', name = "test" ) }}';
}
python中的和函数:

@app.route('/chat/<string:name>')
def chat(name):
    return render_template("chat.html", c=name, s="Janek")
@app.route(“/chat/”)
def聊天室(名称):
返回render_模板(“chat.html”,c=name,s=“Janek”)
但当调用我的第一个(ajax js)时,结果是404未找到错误。删除其他变量名后,everythink将正确加载。我做错了什么

编辑1 整个js是:

<script type=text/javascript>
    $(function() {
        $('a#join_btn').bind('click', function() {
            $.post('/', {
                var1: $('input[name="cname"]').val(),
            }, function(data) {
                $('#result').text(data.result);
                if (data.result == "Successfully joined"){
                    window.location.href='{{ url_for( 'chat', name = "test" ) }}';
                }
            });
            return false;
        });
    });
</script>

$(函数(){
$('a#join_btn').bind('click',function()){
$.post(“/”{
var1:$('input[name=“cname”]”)。val(),
},函数(数据){
$('#result').text(data.result);
如果(data.result==“成功加入”){
window.location.href='{{url_for('chat',name=“test”)}}';
}
});
返回false;
});
});
已解决:
方法=[“POST”]
添加到
@app.route('/')
为我修复了错误。

@app.route('/chat/')
应该是
@app.route('/chat/')

您好,您介意多讲一点上下文吗?JS代码段来自哪里?乍一看,你分享的作品都是我能说出来的。你能看看我的编辑吗?谢谢你的编辑。您提到您的第一个ajax在404上失败了。这是否意味着调用
post(“/”…
)失败?您的问题中没有包含该端点。您对
使用
url\u的行为在我看来非常合法,您的
/chat/
端点在我看来也很正确。我会再次检查响应“/”的端点的路由函数也包括方法post(
@app.route('/',methods=['POST'])
如果404只允许GET请求,但您的JS正在尝试执行POST,则它可能是404的源代码)哦,是的。这包括方法post修复了错误。谢谢你进一步解释你试图做什么?我有一个带有按钮的html页面。我想重定向到带有一些附加值的不同html页面(以便能够使用它,例如,
{{somedata}
)点击按钮是包含有呈现模板()的js的html文件但是这个脚本在我的.html文件
@app.route('/join_chat')def join_chat():return render_模板(“join_chat.html”)