jQuery 如何使用Javascript通过'POST'方法重定向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19064352/
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:07:33  来源:igfitidea点击:

How to redirect through 'POST' method using Javascript?

javascriptjqueryredirectpost

提问by Satheesh

I've queried and doesn't work out what I've found. Is there any way to redirect to give url with POSTmethod using Javascript or jquery?

我已经查询过,但没有弄清楚我发现了什么。有什么方法可以使用 Javascript 或 jquery重定向以使用POST方法提供 url ?

回答by Ebrahim Byagowi

Based on Eugene Naydenov's answer, I ended up using this which is able to fill form data also, hope to be useful for others:

基于Eugene Naydenov的回答,我最终使用了它也可以填写表单数据,希望对其他人有用:

function redirectPost(url, data) {
    var form = document.createElement('form');
    document.body.appendChild(form);
    form.method = 'post';
    form.action = url;
    for (var name in data) {
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = name;
        input.value = data[name];
        form.appendChild(input);
    }
    form.submit();
}

// redirectPost('http://www.example.com', { text: 'text\n\ntext' });

回答by Eugene Naydenov

Create a form, fill methodand actionattributes, submit the form.

创建表单,填写methodaction属性,提交表单。

var redirect = function(url, method) {
    var form = document.createElement('form');
    form.method = method;
    form.action = url;
    form.submit();
};

redirect('http://www.example.com', 'post');


jQuery version (but I'd prefer pure JavaScriptin this particular case):

jQuery 版本(但在这种特殊情况下我更喜欢纯 JavaScript):

var redirect = function(url, method) {
    $('<form>', {
        method: method,
        action: url
    }).submit();
};

redirect('http://www.example.com', 'post');

回答by Fernando

Here the idea is to send data to server while you redirect user to another webpage without using the GET method and maintaining a cosmetic appearance to your URL. So we are going to use the same procedure of sending a Form with POST method.

这里的想法是将数据发送到服务器,同时将用户重定向到另一个网页,而不使用 GET 方法并保持 URL 的外观。所以我们将使用相同的过程来发送带有 POST 方法的表单。

HTML/Javascript code for creating the form and submitting it to server:

用于创建表单并将其提交到服务器的 HTML/Javascript 代码:

<html>
<body>
<script>
var form = document.createElement("form");
form.method = 'post';
form.action = 'receive.php';
var input = document.createElement('input');
input.type = "text";
input.name = "data";
input.value = "this is the data I'm sending to server through POST";
form.appendChild(input);
form.submit();
</script>
<body>
<html>

Receiving this data in PHP:

在 PHP 中接收这些数据:

<pre>
<?php
var_dump($_POST);
?>
</pre>

In this way, users will be redirected to receive.php with some data being informed in a POST method.

通过这种方式,用户将被重定向到 receive.php,并在 POST 方法中通知一些数据。

回答by Guru

Try this:

尝试这个:

var url = 'http://example.com/vote/' + Username;
var form = $('<form action="' + url + '" method="post">' +
  '<input type="text" name="api_url" value="' + Return_URL + '" />' +
  '</form>');
$('body').append(form);
$(form).submit();

回答by Sergey Telshevsky

Actually there is a trick. You create a hidden form and trigger the submit button, with jQuery for example:

其实是有窍门的。您创建一个隐藏的表单并触发提交按钮,例如使用 jQuery:

<form method="POST" action="newurl.html" id="myform">
</form>

and do a

并做一个

$('#myform').submit();

I suggest you to use the no-jQuery version posted by Eugene Naydenov

我建议您使用Eugene Naydenov发布的非 jQuery 版本

回答by sohel shaikh

Redirect with POST vars (on jQuery)

使用 POST 变量重定向(在 jQuery 上)

var redirect = 'http://www.website.com/page?id=23231';
$.redirectPost(redirect, {x: 'example', y: 'abc'});

$.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>').submit();
    }
});

回答by sohel shaikh

You can use serialize your form and all the data in post. Below is an example.

您可以使用序列化您的表单和帖子中的所有数据。下面是一个例子。

$("#submit_btn").click(function(){
        $('.error_status').html();
            if($("form#frm_message_board").valid())
            {
                $.ajax({
                      type: "POST",
                      url: "<?php echo site_url('message_board/add');?>",
                      data: $('#frm_message_board').serialize(),
                      success: function(msg) {
                          var msg = $.parseJSON(msg);
                          if(msg.success=='yes')
                          {
                                                                            return true;
                         }
                         else
                         {
                            alert('Server error');
                            return false;
                        }
                       }
                });
            }
            return false;
        });

回答by renacimiento

I think you should construct and fill out a hidden method=POST action="YOUR_URL" form and submit it,

我认为您应该构建并填写一个隐藏的 method=POST action="YOUR_URL" 表单并提交,

回答by Suresh Atta

There is no way to do so.

没有办法这样做。

Even a aelement Sstrikes the GET.

甚至一个a元素也会对GET.

What you can do is make a Ajax request to post and in on-successuse window.open().

您可以做的是发出一个 Ajax 请求来发布和on-success使用window.open()

回答by arpan desai

Using jQuery post the data and redirect to your desired location like so:

使用 jQuery 发布数据并重定向到您想要的位置,如下所示:

$.ajax({
  type: 'POST',
  url: 'receivedata.asp',
  data: 'formValue=someValue',
  success: function(data){
      window.location = data;
  }
});

You could remove the data: 'formValue=someValue', if there is no data to pass to the page you want to post to.

您可以删除数据:'formValue=someValue',如果没有数据传递到您要发布到的页面。