javascript 在后台提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6877759/
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
Submit a form in the background
提问by Alex Churchill
How can I submit a form to a third-party website without it causing the page to reload (i.e. in the background)?
如何在不导致页面重新加载(即在后台)的情况下向第三方网站提交表单?
For some background, I'm trying to programmatically log a user into their google account in the background.
对于某些背景,我试图在后台以编程方式将用户登录到他们的谷歌帐户。
I can programmatically log them in using the following:
我可以使用以下方法以编程方式登录它们:
var div = document.createElement("div");
div.innerHTML = FORM_HTML;
var form = div.getElementsByTagName("form")[0];
form.action = "https://www.google.com/accounts/ServiceLoginAuth";
form.GALX.value = FORM_GALX; // dynamically obtained using AJAX
form.Email.value = USER_EMAIL;
form.Passwd.value = USER_PASSWORD;
form.submit();
This logs me in and opens up my Google dashboard. How can I prevent form.action
from redirecting me?
这让我登录并打开我的 Google 仪表板。如何防止form.action
重定向我?
Please note that this div is never actually added to a document, which is preferable but not required.
请注意,此 div 从未实际添加到文档中,这是可取的,但不是必需的。
回答by fatnjazzy
you can set the target to submit the form to an IFRAME
like that
您可以设置目标以将表单提交到 IFRAME
这样的
<form target="transFrame" method="POST" action="http://...." ></form>
<iframe style="" name="transFrame" id="transFrame"></iframe>
回答by Barry Kaye
You need to use AJAX to generate XMLHttpRequest best using any popular JS library such as jQuery, Prototype JS or Mootools.
您需要使用 AJAX 生成 XMLHttpRequest,最好使用任何流行的 JS 库,例如 jQuery、Prototype JS 或 Mootools。
回答by Nayan Bagia
you can use ajax or jquery for request to submit form in background if you want to submit form to secure connection i think they want allow submit form in this way..
如果您想提交表单以确保连接安全,您可以使用 ajax 或 jquery 请求在后台提交表单,我认为他们希望以这种方式提交表单..