使用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;
}
我知道你可以发送 POST
带有 jQuery.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 $ c发送
api_url
值$ c>到 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屋!