Send POST data on redirect with JavaScript/jQuery? [duplicate]


Send POST data on redirect with JavaScript/jQuery? [duplicate]

This question already has an answer here:

JavaScript post request like a form submit

29 answers

Basically what I want to do is send POST data when I change the window.location, as if a user has submitted a form and it went to a new page. I need to do it this way because I need to pass along a hidden URL, and I can’t simply place it in the URL as a GET for cosmetic reasons.
This is what I have at the moment, but it doesn’t send any POST data.
if(user has not voted) {

window.location = ‘’ + Username;


I know that you can send POST data with, but I need it to be sent with the new window.location.
So to recap, I need to send api_url value via POST to, while sending the user to the same page at the same time.

For future reference, I ended up doing the following:
if(user has not voted) {






Solution 1:

Construct and fill out a hidden method=POST action="" form and submit it, rather than using window.location at all.

Solution 2:

per @Kevin-Reid’s answer, here’s an alternative to the “I ended up doing the following” example that avoids needing to name and then lookup the form object again by constructing the form specifically (using jQuery)..

var url = '' + Username;
var form = $('<form action="' + url + '" method="post">' +
  '<input type="text" name="api_url" value="' + Return_URL + '" />' +

Solution 3:

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

var redirect = '';
$.redirectPost(redirect, {x: 'example', y: 'abc'});

// jquery extend function
    redirectPost: function(location, args)
        var form = '';
        $.each( args, function( key, value ) {
            value = value.split('"').join('\"')
            form += '<input type="hidden" name="'+key+'" value="'+value+'">';
        $('<form action="' + location + '" method="POST">' + form + '</form>').appendTo($(document.body)).submit();

Solution 4:

If you are using jQuery, there is a redirect plugin that works with the POST or GET method. It creates a form with hidden inputs and submits it for you. An example of how to get it working:

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

Note: You can pass the method types GET or POST as an optional third parameter; POST is the default.

Solution 5:

Here is a method, which does not use jQuery.
I used it to create a bookmarklet, which checks the current page on w3-html-validator.

var f = document.createElement('form');

var i=document.createElement('input');
i.value='<!DOCTYPE html>'+document.documentElement.outerHTML;


Solution 6:

The answers here can be confusing so i will give you a sample code that i am working with.

To start with note that there is no POST parameter to java script windows.location function that you are referring to.

So you have to…

  1. Dynamically make a form with a POST parameter.

  2. Dynamically put a textbox or textboxes with your desired values to post

  3. Invoke the submit form you dynamically created.

And for the example.

     //---------- make sure to link to your jQuery library ----//

<script type="text/javascript" >

    var form = $(document.createElement('form'));
    $(form).attr("action", "test2.php");
    $(form).attr("method", "POST");
    $(form).css("display", "none");

    var input_employee_name = $("<input>")
    .attr("type", "text")
    .attr("name", "employee_name")
    .val("Peter" );

    var input_salary = $("<input>")
    .attr("type", "text")
    .attr("name", "salary")
    .val("1000" );

    form.appendTo( document.body );


If all is done well, you shall be redirected to test2.php and you can use POST to read passed values of employee_name and salary; that will be Peter and 1000 respectively.

On test2.php you can get your values thus.

$employee_name = $_POST['employee_name'];
$salary = $_POST['salary'];

Needless to say , make sure you sanitize your passed values.