虚拟表单提交模拟ajax请求

来源:互联网 发布:淘宝卖家怎么添加商品 编辑:程序博客网 时间:2024/05/20 12:50
前端开发人员经常会遇到ajax请求出现跨域问题,比如做单点登录。结合本人开发经验推荐一方法——虚拟表单提交,解决此类问题。此方法的特点是简单易懂!好了,直接上干货!
function formCommit(){
var formobj;
// window.top.open(url,"_blank");
$.ajax({
type: 'post',
dataType : 'jsonp',
url: '某个具体系统的url',
success:function(data){
// 虚拟化一个表单,放在一个虚拟化的iframe中,后提交
formobj = $("<form style='display:none' id='formMap' target='_self'  action='某个具体系统的url' method='post'><input name='username' value='用户名'/><input name='password' value='密码'/><input name='jsonp' value='true'/><input name='token' value='"+data+"'/></form>");
// 虚拟化一个iframe,承载虚拟化表单
var tempIframe = $("<iframe style='display:none' src='登录页面统一路径下任何一个jsp或html文件' id='tempIframe' name='tempIframe' />");
// body中添加虚拟化的iframe
$("body:eq(0)").append(tempIframe);
            // 设置iframe的src
$("#tempIframe").attr("src", "AYKJ.GISDevelopTestPage.html");
// 获取iframe中的内容
var mainIframe = (document.getElementById("tempIframe").contentDocument || document.getElementById("tempIframe").contentWindow.document);
// iframe中添加表单
$(mainIframe.body).append($(formobj));
// 在iframe中找到表单并提交
$(mainIframe).find("#formMap").submit();

setTimeout(function(){  
window.parent.open("系统中具体的某个页面的url","_blank"); 
},1500);
0 0
原创粉丝点击