Flask redirect(urlfor()) doesn't reload the page

This question already has an answer here:

  • How to manage a redirect request after a jQuery Ajax call 30 answers

  • AJAX cannot handle a redirect response, afaik. You could, though, encapsulate {{message}} in a <span id="message"> , identify the element, and do a jQuery .html() to insert custom text upon AJAX completion success.

    $.ajax({
       type: "POST",
       contentType: "application/json; charset=utf-8",
       url: "/second_route",
       data: JSON.stringify({}),
       success: [],
       dataType: "json"
    }).done(function(){
       $('#message').html('test');
       window.location.href = '/';
    }).fail(function(response){
       $('#message').html(response['responseText']);
    });
    

    To redirect, you'd replace the code inside the done clause with window.location.href = "/someURL" , but since the endpoint is the same this isn't necessary. This way, using a message variable at all wouldn't be necessary.


    This is how I ended up solving the issue for those reading the question.

    Changing my post request to post to the same route the loads the page with this call:

     var posting = $.post(window.location.href, {'some key': 'some value'});
                    posting.done(function( ) {
                        window.location.replace(window.location.href);
                    });
    
    链接地址: http://www.djcxy.com/p/12468.html

    上一篇: 未执行Ajax重定向

    下一篇: Flask重定向(urlfor())不会重新加载页面