Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

javascript - 使用JavaScript / jQuery在重定向上发送POST数据? [重复](Send POST data on redirect with JavaScript/jQuery? [duplicate])

This question already has an answer here:(这个问题在这里已有答案:)

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.(基本上我想要做的是在我更改window.location时发送POST数据,就像用户提交了一个表单并进入新页面一样。)

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.(我需要这样做,因为我需要传递一个隐藏的URL,而且出于美观的原因,我不能简单地将它作为GET放在URL中。)

This is what I have at the moment, but it doesn't send any POST data.(这就是我目前所拥有的,但它不会发送任何POST数据。)

if(user has not voted) {

    window.location = 'http://example.com/vote/' + Username;

}

I know that you can send POST data with jQuery.post() , but I need it to be sent with the new window.location .(我知道你可以用jQuery.post()发送POST数据,但我需要用新的window.location发送它。)

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.(所以回顾一下,我需要通过POSTapi_url值发送到http://example.com/vote/ ,同时将用户同时发送到同一页面。)


For future reference, I ended up doing the following :(为了将来参考,我最终做了以下事情 :)

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();

}
  ask by Josh Foskett translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

per @Kevin-Reid's answer, here's an alternative to the "I ended up doing the following" example that avoids needing to name and then lookup the form object again by constructing the form specifically (using jQuery)..(根据@ Kevin-Reid的回答,这里是“我最终做了以下”示例的替代方法,它避免了需要命名,然后通过专门构造表单(使用jQuery)再次查找表单对象。)

var url = 'http://example.com/vote/' + Username;
var form = $('<form action="' + url + '" method="post">' +
  '<input type="text" name="api_url" value="' + Return_URL + '" />' +
  '</form>');
$('body').append(form);
form.submit();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...