How to redirect with JQuery, load another page but sent some POST parameters in request?

ε祈祈猫儿з 提交于 2019-11-29 21:26:08

问题


How do I redirect using either JQuery, DOJO or plain JavaScript, load another page but sent some POST parameters in request?


回答1:


This should work, but i haven't 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 can not POST stuff directly from script.




回答2:


You can't redirect with postdata using JavaScript, and I'm sure it's the same for jQuery as well. However, what you can do is...have a hidden form with post data, manipulate it as you need in javascript and submit that form.

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



回答3:


You can do first the POST request to 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




回答4:


Voici ma proposition :

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 chargement, on récupère requeteur.responseText.

A vos commentaires !

Sacapuss




回答5:


use this code

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


来源:https://stackoverflow.com/questions/6439868/how-to-redirect-with-jquery-load-another-page-but-sent-some-post-parameters-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!