简体   繁体   English

通过javascript将表单提交到2个不同的网站

[英]submitting form to 2 different websites second via javascript

I have a situation where I want to post form data to 2 diffeent locations at the same time. 我有一种情况,我想同时将表单数据发布到2个不同的位置。 I do not have control of the locations and they are cross domain. 我无法控制位置,而且它们是跨域的。 I found a previous post here, using an iframe, that I can't find now, that I have incorporated. 我在这里找到了以前使用iframe的帖子,该帖子现在已经找不到,我已经将其合并。 It submits to the first one every time but the second one only gets submitted 25% of the time. 它每次都提交给第一个,但是第二个提交的时间只有25%。 I need it to work much more reliably in all the main browsers. 我需要它在所有主要浏览器中都能更可靠地工作。 Tried using ajax but could not get it to work cross domain in IE trying many solutions posed in this forum. 尝试使用ajax,但尝试在此论坛中提出的许多解决方案无法使其在IE中跨域工作。 Thanks. 谢谢。 Ed 埃德

<form name="myForm" method="post" action="http://www.firstwebsite.com" onsubmit="return submit2nd()"  >
<input type="email" name="email" value='' />
<input type="submit" name="submit" />
</form>
<script>

function submit2nd(){
var x=document.forms["myForm"]["email"].value;
var iframe = document.createElement("iframe");
var uniqueString = "asderwegthyuq123";
document.body.appendChild(iframe);
iframe.style.display = "none";
iframe.contentWindow.name = uniqueString;
var form = document.createElement("form");
form.target = uniqueString;
form.action = "http://www.secondwebsite.com";
form.method = "POST";
var input = document.createElement("input");
input.type = "hidden";
input.name = "email";
input.value = x;
form.appendChild(input);
document.body.appendChild(form);
form.submit();
return true;
}
</script>    

Duplicate the form upon submission and submit the forms to two iframes with different destinations, or use AJAX. 提交后复制表格,然后将表格提交到目的地不同的两个iframe,或使用AJAX。 I highly recommend using frameworks such as JQuery so you need not worry about compatibility issues. 我强烈建议使用诸如JQuery之类的框架,因此您不必担心兼容性问题。

What's happening to your code is that, it will attempt to submit to the second page during the submission of the first page. 您的代码正在发生的事情是,它将在提交第一页期间尝试将其提交到第二页。 If the submission to the first page finishes before its submission to the second, the second submission will not be successful. 如果对第一页的提交在完成对第二页的提交之前完成,则第二次提交将不会成功。 All scripts in the current page will stop if you leave the page (because you will be going to www.firstwebsite.com). 如果您离开该页面,则当前页面中的所有脚本都将停止(因为您将访问www.firstwebsite.com)。

One solution you can do here is: 您可以在此处执行的一种解决方案是:

  1. Change submit2nd() so that it will submit for the first and second website. 更改submit2nd()以便它将为第一个和第二个网站提交。
  2. submit2nd() should return false so it will not attempt to submit to the page in the action attribute. submit2nd()应该返回false,因此它不会尝试提交到action属性中的页面。
  3. Upon submission (clicking of the submit button), duplicate the form. 提交后(单击提交按钮),复制表格。
  4. First form -> change action to www.firstwebsite.com -> submit. 第一个表单->将action更改为www.firstwebsite.com->提交。
  5. Second form -> change action to www.secondwebsite.com -> submit. 第二种形式->将action更改为www.secondwebsite.com->提交。

Note: If you wish to use AJAX for this, just make sure that you will not have cross domain issues. 注意:如果您希望为此使用AJAX,只需确保您没有跨域问题。 In your example, you tried submitting the form to two different domains: firstwebsite.com and secondwebsite.com. 在您的示例中,您尝试将表单提交到两个不同的域:firstwebsite.com和secondwebsite.com。 You need special settings for this if they are on different domains. 如果它们位于不同的域上,则需要为此进行特殊设置。 You can read more about this or check the explanation of this policy. 您可以阅读有关此内容的更多信息或查看此政策的说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM