Javascript 使用 window.location.href 传递发布数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2367979/
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
pass post data with window.location.href
提问by Brian
When using window.location.href, I'd like to pass POST data to the new page I'm opening. is this possible using JavaScript and jQuery?
使用 window.location.href 时,我想将 POST 数据传递到我打开的新页面。这可以使用 JavaScript 和 jQuery 吗?
回答by Guffa
Using window.location.hrefit's not possible to send a POST request.
使用window.location.href它是不可能发送 POST 请求的。
What you have to do is to set up a formtag with data fields in it, set the actionattribute of the form to the URL and the methodattribute to POST, then call the submitmethod on the formtag.
您需要做的是设置一个form带有数据字段的标签action,将表单的属性设置为 URL,将method属性设置为 POST,然后调用标签submit上的方法form。
回答by Mohamed Khamis
Add a form to your HTML, something like this:
在 HTML 中添加一个表单,如下所示:
<form style="display: none" action="/the/url" method="POST" id="form">
<input type="hidden" id="var1" name="var1" value=""/>
<input type="hidden" id="var2" name="var2" value=""/>
</form>
and use JQuery to fill these values (of course you can also use javascript to do something similar)
并使用 JQuery 来填充这些值(当然你也可以使用 javascript 来做类似的事情)
$("#var1").val(value1);
$("#var2").val(value2);
Then finally submit the form
然后最后提交表单
$("#form").submit();
on the server side you should be able to get the data you sent by checking var1and var2, how to do this depends on what server-side language you are using.
在服务器端,您应该能够通过检查var1和获取发送的数据var2,如何执行此操作取决于您使用的服务器端语言。
回答by Mohd Tauovir Khan
Use this file : "jquery.redirect.js"
使用这个文件:“jquery.redirect.js”
$("#btn_id").click(function(){
$.redirect(http://localhost/test/test1.php,
{
user_name: "khan",
city : "Meerut",
country : "country"
});
});
});
回答by Aquajet
As it was said in other answers there is no way to make a POST request using window.location.href, to do it you can create a form and submit it immediately.
正如在其他答案中所说,无法使用 window.location.href 发出 POST 请求,为此您可以创建一个表单并立即提交。
You can use this function:
您可以使用此功能:
function postForm(path, params, method) {
method = method || 'post';
var form = document.createElement('form');
form.setAttribute('method', method);
form.setAttribute('action', path);
for (var key in params) {
if (params.hasOwnProperty(key)) {
var hiddenField = document.createElement('input');
hiddenField.setAttribute('type', 'hidden');
hiddenField.setAttribute('name', key);
hiddenField.setAttribute('value', params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
postForm('mysite.com/form', {arg1: 'value1', arg2: 'value2'});
回答by joequincy
Short answer: no. window.location.hrefis not capable of passing POST data.
简短的回答:没有。window.location.href无法传递 POST 数据。
Somewhat more satisfying answer: You can use this function to clone all your form data and submit it.
更令人满意的答案:您可以使用此功能克隆所有表单数据并提交。
var submitMe = document.createElement("form");
submitMe.action = "YOUR_URL_HERE"; // Remember to change me
submitMe.method = "post";
submitMe.enctype = "multipart/form-data";
var nameJoiner = "_";
// ^ The string used to join form name and input name
// so that you can differentiate between forms when
// processing the data server-side.
submitMe.importFields = function(form){
for(k in form.elements){
if(input = form.elements[k]){
if(input.type!="submit"&&
(input.nodeName=="INPUT"
||input.nodeName=="TEXTAREA"
||input.nodeName=="BUTTON"
||input.nodeName=="SELECT")
){
var output = input.cloneNode(true);
output.name = form.name + nameJoiner + input.name;
this.appendChild(output);
}
}
}
}
- Do
submitMe.importFields(form_element);for each of the three forms you want to submit. - This function will add each form's name to the names of its child inputs (If you have an
<input name="email">in<form name="login">, the submitted name will belogin_name. - You can change the
nameJoinervariable to something other than_so it doesn't conflict with your input naming scheme. - Once you've imported all the necessary forms, do
submitMe.submit();
submitMe.importFields(form_element);为您要提交的三个表格中的每一个做。- 此函数会将每个表单的名称添加到其子输入的名称中(如果您有
<input name="email">in<form name="login">,则提交的名称将为login_name. - 您可以将
nameJoiner变量更改为其他内容,_以免与您的输入命名方案冲突。 - 导入所有必要的表格后,请执行
submitMe.submit();
回答by Sergio Roldan
it's as simple as this
就这么简单
$.post({url: "som_page.php",
date: { data1: value1, data2: value2 }
).done(function( data ) {
$( "body" ).html(data);
});
});
I had to solve this to make a screen lock of my application where I had to pass sensitive data as user and the url where he was working. Then create a function that executes this code
我必须解决这个问题才能对我的应用程序进行屏幕锁定,我必须将敏感数据作为用户和他工作的 url 传递。然后创建一个执行此代码的函数
回答by Down_with_opp
Have you considered simply using Local/Session Storage? -or- Depending on the complexity of what you're building; you could even use indexDB.
您是否考虑过简单地使用本地/会话存储?- 或 - 取决于您正在构建的内容的复杂性;你甚至可以使用 indexDB。
note:
注意:
Local storageand indexDBare not secure - so you want to avoid storing any sensitive / personal data (i.e names, addresses, emails addresses, DOB etc) in either of these.
Local storage并且indexDB不安全 - 因此您希望避免在其中任何一个中存储任何敏感/个人数据(即姓名、地址、电子邮件地址、出生日期等)。
Session Storageis a more secure option for anything sensitive, it's only accessible to the origin that set the items and also clears as soon as the browser / tab is closed.
Session Storage是任何敏感内容的更安全选项,它只能由设置项目的源访问,并且在浏览器/选项卡关闭后立即清除。
IndexDBis a little more [but not much more] complicated and is a 30MB noSQL databasebuilt into every browser (but can be basically unlimited if the user opts in) -> next time you're using Google docs, open you DevTools -> application -> IndexDB and take a peak. [spoiler alert: it's encrypted].
IndexDB有点[但不是更复杂],并且30MB noSQL database内置于每个浏览器中(但如果用户选择加入,则基本上可以不受限制)-> 下次您使用 Google 文档时,打开您的 DevTools -> 应用程序 -> IndexDB并采取一个高峰。[剧透警报:它是加密的]。
Focusing on Localand Session Storage; these are both dead simple to use:
专注于Local和Session Storage;这些都非常易于使用:
// To Set
sessionStorage.setItem( 'key' , 'value' );
// e.g.
sessionStorage.setItem( 'formData' , { name: "Mr Manager", company: "Bluth's Frozen Bananas", ... } );
// Get The Data
const fromData = sessionStorage.getItem( 'key' );
// e.g. (after navigating to next location)
const fromData = sessionStorage.getItem( 'formData' );
// Remove
sessionStorage.removeItem( 'key' );
// Remove _all_ saved data sessionStorage
sessionStorage.clear( );
If simple is not your thing -or- maybe you want to go off roadand try a different approach all together -> you can probably use a shared web worker... y'know, just for kicks.
如果简单不是你的事 - 或者 - 也许你想离开道路并一起尝试不同的方法 - >你可能可以使用shared web worker......你知道,只是为了踢球。
回答by Kissa Mia
You can use GET instead of pass, but don't use this method for important values,
您可以使用 GET 代替 pass,但不要对重要值使用此方法,
function passIDto(IDval){
window.location.href = "CustomerBasket.php?oridd=" + IDval ;
}
In the CustomerBasket.php
在 CustomerBasket.php 中
<?php
$value = $_GET["oridd"];
echo $value;
?>

