How to redirect using jQuery, load another page, but send some POST parameters to the request?

How to redirect using jQuery, DOJO or simple JavaScript, load another page, but sent some POST parameters to the request?

+5
source share
5 answers

This should work, but I have not tested it:

function postData(url, data)
{
  var form = $('<form></form>');
  $(form).hide().attr('method','post').attr('action',url);
  for (i in data)
  {
    var input = $('<input type="hidden" />').attr('name',i).val(data[i]);
    $(form).append(input);
  }
  $(form).appendTo('body').submit();
}

Basically, you can create a form on the fly and submit it. Unfortunately, you cannot use POST directly from a script.

+10
source

postdata JavaScript, , jQuery. , , ... , , javascript, .

<form id="myform" method="post" action="mypage.php">
    <input id="myinput" type="hidden" value="mydata" /> 
</form>

<script type="text/javascript">
    // in some function...
    document.getElementById("myform").submit();
</script>
+1

You can first execute a POST request on the page and then redirect:

var urlToPost='http://www.example.com/';
$.post(urlToPost, {'PARAMETER':'VALUE', 'PARAMETER2': 'VALUE2' /*Etc.*/}, function(response){
/*Callback*/
alert(response);
document.location=urlToPost;
})

More info for this: jQuery Documentation

0
source

Voici ma offer:

String.prototype.poster=function() 
{
    var requeteur=new XMLHttpRequest;
    requeteur.open('post',this,true); 
    requeteur.setRequestHeader('Content-type','application/x-www-form-urlencoded');

    var equations=[]; 
    for(i in arguments) equations.push(arguments[i]);
    requeteur.send(equations.join(esper));
}

Au fee, at récupère requeteur.responseText.

Voted vos!

Sacapuss

0
source

use this code

$().redirect('demo.php', {'arg1': 'value1', 'arg2': 'value2'});
Run codeHide result
0
source

All Articles