jQuery - Redirect with post data

前端 未结 12 1576
被撕碎了的回忆
被撕碎了的回忆 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 20:51

    Here's a simple small function that can be applied anywhere as long as you're using jQuery.

    var redirect = 'http://www.website.com/page?id=23231';
    $.redirectPost(redirect, {x: 'example', y: 'abc'});
    
    // jquery extend function
    $.extend(
    {
        redirectPost: function(location, args)
        {
            var form = '';
            $.each( args, function( key, value ) {
                form += '';
            });
            $('
    '+form+'
    ').appendTo('body').submit(); } });

    Per the comments, I have expanded upon my answer:

    // jquery extend function
    $.extend(
    {
        redirectPost: function(location, args)
        {
            var form = $('
    '); form.attr("method", "post"); form.attr("action", location); $.each( args, function( key, value ) { var field = $(''); field.attr("type", "hidden"); field.attr("name", key); field.attr("value", value); form.append(field); }); $(form).appendTo('body').submit(); } });

提交回复
热议问题