How to redirect through 'POST' method using Javascript?

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

Answers:

Answer

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:

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' });
Answer

Create a form, fill method and action attributes, submit the form.

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 JavaScript in this particular case):

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

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

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.

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

<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:

<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.

Answer

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

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

<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

Answer

Redirect with POST vars (on 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();
    }
});
Answer

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

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

Answer

There is no way to do so.

Even a a element Sstrikes the GET.

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

Answer

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

$.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.

Answer

huh... use AJAX Jquery:

> $.ajax({
>                       type: "POST",
>                       url: "www.example.com/the_page_I_post_to.php",
>                       data: $('yourform').serialize(),//sent data
>                       success: function(msg) {
>                           alert("posted !");
>                        }

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.