您的位置:首页 > Web前端 > JQuery

解决Jquery ajax的post get跨域提交表单

2013-05-10 14:35 447 查看
跨域的N种形式:

1.直接用jquery中$.getJSON进行跨域提交

优点:有返回值,可直接跨域;

缺点:数据量小;

提交方式:仅get (无$.postJSON)

[javascript]
view plaincopyprint?

$.getJSON("http://www.sendnet.cn/?callback=?" , { UserId: 1001 },
nction (data) {
alert(data.info);
;

$.getJSON("http://www.sendnet.cn/?callback=?" , { UserId: 1001 },
function (data) {
alert(data.info);
});


[javascript]
view plaincopyprint?

$.ajax({
type: "Get", or 'POST'
url: "http://www.sendnet.cn/?UserId=1001",
cache: false,
error: function () { },
jsonp: "callback",
dataType: "jsonp",
success: function (result) {
alert(result.info);
}
});

我这边试过 get post 都可以
$.ajax({
type: "Get",
url: "http://www.sendnet.cn/?UserId=1001",
cache: false,
error: function () { },
jsonp: "callback",
dataType: "jsonp",
success: function (result) {
alert(result.info);
}
});


2.在页面中嵌入一个iframe,把iframe的宽和高设置为0进行跨域提交

优点:可直接跨域;

缺点:无返回值(脱离ajax本质);

提交方式:get/post

[plain]
view plaincopyprint?

使用隐藏的iframe来提交表单
1,在页面中嵌入一个iframe,把iframe的宽和高设置为0
2.在iframe的里面里设置一个from的表单,表单的内容就是真正要提交的表单内容。
3.当点击按钮的时候是iframe里的表单提交。

<form id="form2" name="form2" method="post" action="a,jsp" enctype="multipart/form-data">
<input name="option_13412" id="option_13412" type="text"/>
<input name="option_13413" id="option_13413" type="text"/>
<input name="option_13414" id="option_13414" type="text"/>
<input name="option_13415" id="option_13415" type="text"/>
</form>

使用jquery来啊操作iframe中的表单元素
$(window.frames["iframe1"].document).find("#option_13412").val(name);
$(window.frames["iframe1"].document).find("#option_13413").val(phone);
$(window.frames["iframe1"].document).find("#option_13415").val(content);

通过按钮来提交iframe里的表单
$(window.frames["iframe1"].document).find("#form2").submit();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: