问题描述
如何在没有ajax(跨网域问题)的情况下将一个表单提交到两个不同的位置
我在想下面的内容。只有我如何传递参数?
< form action =urlOne.com/somethingmethod =postonsubmit =postToUrl(urlTwo.com/something);>
< input type =textvalue =helloname =hi/>
< input type =submitvalue =submit>
< / form>
从这里取得的函数>>
function postToUrl(url,params)
{
var form = $('< form>')
form.attr('action',url);
form.attr('method','POST');
var addParam = function(paramName,paramValue){
var input = $('< input type =hidden>');
input.attr({'id':paramName,
'name':paramName,
'value':paramValue});
form.append(input);
};
// Params是一个数组。
if(params instanceof Array){
for(var i = 0; i< params.length; i ++){
addParam(i,params [i]);
}
}
// Params是一个关联数组或对象。
if(params instanceof Object){
for(var key in params){
addParam(key,params [key]);
}
}
//提交表单,然后从页面中删除
form.appendTo(document.body);
form.submit();
form.remove();
}
谢谢。
您只能在窗口中一次过发布一个表单。如果您尝试发布两个表单,一个帖子会停止另一个。
解决方案是将两个表单发布到不同的窗口。您可以将第一个表单的目标设置为在网页中的iframe或新窗口中发布:
form.attr ('target','_blank');
这样,这两个帖子将在单独的窗口中加载,并且不会停止。 p>
how would I submit one form to two different location without ajax (cross-domain issue)
I was thinking of something like below. Only how would I pass params ?
< form action="urlOne.com/something" method="post" onsubmit="postToUrl("urlTwo.com/something");">
<input type="text" value="hello" name="hi"/>
<input type="submit" value="submit">
</form>
function taken from here >> JavaScript post request like a form submit
function postToUrl(url, params)
{
var form = $('<form>');
form.attr('action', url);
form.attr('method', 'POST');
var addParam = function(paramName, paramValue){
var input = $('<input type="hidden">');
input.attr({ 'id': paramName,
'name': paramName,
'value': paramValue });
form.append(input);
};
// Params is an Array.
if(params instanceof Array){
for(var i=0; i<params.length; i++){
addParam(i, params[i]);
}
}
// Params is an Associative array or Object.
if(params instanceof Object){
for(var key in params){
addParam(key, params[key]);
}
}
// Submit the form, then remove it from the page
form.appendTo(document.body);
form.submit();
form.remove();
}
Thank you.
You can only post one form at a time in a window. If you try to post two forms, one post will stop the other.
The solution would be to post the two forms to different windows. You can set the target of the first form to post to an iframe in the page or to a new window:
form.attr('target', '_blank');
This way the two posts would load in separate windows, and wouldn't stop each other.
这篇关于提交一个窗体到两个不同的URL没有ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!