jQuery - 使用帖子数据重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19036684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:05:07  来源:igfitidea点击:

jQuery - Redirect with post data

javascriptjqueryhtmlpost

提问by y2k

How can I redirect with post data?

如何使用帖子数据重定向?

How to move to new page with $_POST?

如何移动到新页面$_POST

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

这该怎么做?它是如何完成的,为什么要完成

回答by Tim S

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.

有一个 JQuery 插件几乎可以完成您想要做的事情: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:

在包含 JQuery 和 jquery.redirect.min.js 插件之后,您可以简单地执行以下操作:

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

Use the following code on newer JQuery versions instead:

在较新的 JQuery 版本上使用以下代码:

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

Hope this helps!

希望这可以帮助!

回答by tfont

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

这是一个简单的小函数,只要您使用 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 += '<input type="hidden" name="'+key+'" value="'+value+'">';
        });
        $('<form action="'+location+'" method="POST">'+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></form>');
        form.attr("method", "post");
        form.attr("action", location);

        $.each( args, function( key, value ) {
            var field = $('<input></input>');

            field.attr("type", "hidden");
            field.attr("name", key);
            field.attr("value", value);

            form.append(field);
        });
        $(form).appendTo('body').submit();
    }
});

回答by kao3991

Why dont just create a form with some hidden inputs and submit it using jQuery? Should work :)

为什么不创建一个带有一些隐藏输入的表单并使用 jQuery 提交它?应该管用 :)

回答by sagie212

Before document/window ready add "extend" to jQuery :

在文档/窗口准备好之前,将“扩展”添加到 jQuery :

$.extend(
{
    redirectPost: function(location, args)
    {
        var form = '';
        $.each( args, function( key, value ) {

            form += '<input type="hidden" name="'+value.name+'" value="'+value.value+'">';
            form += '<input type="hidden" name="'+key+'" value="'+value.value+'">';
        });
        $('<form action="'+location+'" method="POST">'+form+'</form>').submit();
    }
});

Use :

用 :

$.redirectPost("someurl.com", $("#SomeForm").serializeArray());

Note : this method cant post files .

注意:此方法不能发布文件。

回答by Prashant Singh

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 !

这几乎是即时的,用户将看不到任何东西!

回答by Alan

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();
    }
});

回答by Tobiloba

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

.submit() 函数自动提交到 url
.remove() 函数在提交后杀死表单

回答by LameCoder

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?

这需要澄清。您的服务器是否正在处理您想重定向到其他地方的 POST?或者您想将 regulatr GET 请求重定向到另一个需要 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.

制作一个链接,上面写着“如果没有自动重定向,请单击此处”来处理弹出窗口阻止程序,这可能是一个好主意。

回答by kpatrickos

I included the jquery.redirect.min.js plugin in the head section together with this json solution to submit with data

我在头部部分包含了 jquery.redirect.min.js 插件以及这个 json 解决方案以提交数据

<script type="text/javascript">     
    $(function () {
     $('form').on('submit', function(e) {
       $.ajax({
        type: 'post',
        url: 'your_POST_URL',
        data: $('form').serialize(),
        success: function () {
         // now redirect
         $().redirect('your_POST_URL', {
          'input1': $("value1").val(), 
          'input2': $("value2").val(),
      'input3': $("value3").val()
       });
      }
   });
   e.preventDefault();
 });
 });
 </script>

Then immediately after the form I added

然后在我添加的表单之后立即

 $(function(){
     $( '#your_form_Id' ).submit();
    });

回答by Daniel Kennedy

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

构建并填写一个隐藏的 method=POST action="http://example.com/vote" 表单并提交它,而不是使用 window.location。

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();