javascript 如何使用 JQuery 重定向,加载另一个页面但在请求中发送了一些 POST 参数?

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

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

javascriptjquerydojo

提问by Danka

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

如何使用 JQuery、DOJO 或普通 JavaScript 进行重定向、加载另一个页面但在请求中发送了一些 POST 参数?

回答by Ivan

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.

基本上,您可以即时创建表单并提交。不幸的是,您不能直接从脚本发布内容。

回答by Saad Imran.

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.

您不能使用 JavaScript 使用 postdata 进行重定向,我相信 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>

回答by Ryan Casas

You can do first the POST request to the page, and then redirect:

您可以先对页面执行 POST 请求,然后重定向:

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

更多信息:jQuery 文档

回答by Sacapuss

Voici ma proposition :

Voici ma 命题:

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.

Au 收费,关于 récupère requeteur.responseText。

A vos commentaires !

一个 vos 评论员!

Sacapuss

萨卡普斯

回答by user6632299

use this code

使用此代码

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