使用 JavaScript/jQuery 在重定向时发送 POST 数据? [英] Send POST data on redirect with JavaScript/jQuery?
问题描述
基本上我想要做的是在更改 window.location
时发送 POST
数据,就好像用户提交了一个表单并转到了一个新页面.我需要这样做,因为我需要传递一个隐藏的 URL,并且出于美观的原因,我不能简单地将它作为 GET
放在 URL 中.
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.
这是我目前所拥有的,但它不发送任何 POST 数据.
This is what I have at the moment, but it doesn’t send any POST data.
if(user has not voted) {
window.location = 'http://example.com/vote/' + Username;
}
我知道您可以使用 jQuery.post()
发送 POST
数据,但我需要使用新的 window.location> 发送数据代码>.
I know that you can send POST
data with jQuery.post()
, but I need it to be sent with the new window.location
.
所以回顾一下,我需要通过 POST
将 api_url
值发送到 http://example.com/vote/
,同时发送用户同时访问同一页面.
So to recap, I need to send api_url
value via POST
to http://example.com/vote/
, while sending the user to the same page at the same time.
为了将来参考,我最终做了以下事情:
if(user has not voted) {
$('#inset_form').html('<form action="http://example.com/vote/' + Username + '" name="vote" method="post" style="display:none;"><input type="text" name="api_url" value="' + Return_URL + '" /></form>');
document.forms['vote'].submit();
}
推荐答案
构造并填写隐藏的 method=POST action="http://example.com/vote"
表单并提交,而不是使用 window.location
.
Construct and fill out a hidden method=POST action="http://example.com/vote"
form and submit it, rather than using window.location
at all.
这篇关于使用 JavaScript/jQuery 在重定向时发送 POST 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!