jQuery - Redirect with post data

前端 未结 12 1568
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 20:14

How can I redirect with post data?

How to move to new page with $_POST?

How to do this? How is it done and whyfore shall it be done

相关标签:
12条回答
  • 2020-11-28 21:02

    I think this is the best way to do it !

    <html>
    <body onload="document.getElementById('redirectForm').submit()">
    <form id='redirectForm' method='POST' action='/done.html'>
    <input type='hidden' name='status' value='complete'/>
    <input type='hidden' name='id' value='0u812'/>
    <input type='submit' value='Please Click Here To Continue'/>
    </form>
    </body>
    </html>
    

    This will be almost instantaneous and user won't see anything !

    0 讨论(0)
  • 2020-11-28 21:03

    There is a JQuery plug-in that accomplishes pretty much what you're trying to do: https://github.com/mgalante/jquery.redirect/blob/master/jquery.redirect.js.

    After including JQuery and the jquery.redirect.min.js plug-in, you can simply do something like this:

    $().redirect('demo.php', {'arg1': 'value1', 'arg2': 'value2'});
    

    Use the following code on newer JQuery versions instead:

    $.redirect('demo.php', {'arg1': 'value1', 'arg2': 'value2'});
    

    Hope this helps!

    0 讨论(0)
  • 2020-11-28 21:04

    This needs clarification. Is your server handling a POST that you want to redirect somewhere else? Or are you wanting to redirect a regulatr GET request to another page that is expecting a POST?

    In either case what you can do is something like this:

    var f = $('<form>');
    $('<input>').attr('name', '...').attr('value', '...');
    //after all fields are added
    f.submit();
    

    It's probably a good idea to make a link that says "click here if not automatically redirected" to deal with pop-up blockers.

    0 讨论(0)
  • 2020-11-28 21:04

    Construct and fill out a hidden method=POST action="http://example.com/vote" form and submit it, rather than using window.location at all.

    or

    $('#inset_form').html(
       '<form action="url" name="form" method="post" style="display:none;">
        <input type="text" name="name" value="' + value + '" /></form>');
    document.forms['form'].submit();
    
    0 讨论(0)
  • 2020-11-28 21:05

    Similar to the above answer, but written differently.

    $.extend(
    {
        redirectPost: function (location, args) {
            var form = $('<form>', { action: location, method: 'post' });
            $.each(args,
                function (key, value) {
                    $(form).append(
                        $('<input>', { type: 'hidden', name: key, value: value })
                    );
                });
            $(form).appendTo('body').submit();
        }
    });
    
    0 讨论(0)
  • 2020-11-28 21:09

    This would redirect with posted data

    $(function() {
           $('<form action="url.php" method="post"><input type="hidden" name="name" value="value1"></input></form>').appendTo('body').submit().remove();
        });
    
        }
    

    the .submit() function does the submit to url automatically
    the .remove() function kills the form after submitting

    0 讨论(0)
提交回复
热议问题