六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 30|回复: 0

ajax方式提交form

[复制链接]

升级  20%

2

主题

2

主题

2

主题

童生

Rank: 1

积分
10
 楼主| 发表于 2013-1-29 10:35:15 | 显示全部楼层 |阅读模式
思路:
1.创建一个iframe
  if(window.ActiveXObject)
  {
       var io = document.createElement('<iframe id="' + frameId  + '" name="' + frameId + '" />');
      if(typeof uri== 'boolean'){
         io.src = 'javascript:false';
      }
      else if(typeof uri== 'string'){
         io.src = uri;
      }
  }
  else
  {
     var io = document.createElement('iframe');
     io.id = frameId;
     io.name = frameId;
  }
  io.style.position = 'absolute';
  io.style.top = '-1000px';
  io.style.left = '-1000px';
  document.body.appendChild(io);
2.将form的target设置为步骤1创建的iframe
3.为步骤1的iframe注册事件
  if(window.attachEvent)
  {
     document.getElementById(frameId).attachEvent('onload',       uploadCallback);
  }
  else
  {
     document.getElementById(frameId).addEventListener('load', uploadCallback, false);
  }
4.获得返回的数据
  var uploadCallback = function()
   {  
      var io = document.getElementById(frameId);
      try {   
    if(io.contentWindow)
    {
xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
      
    }
         else if(io.contentDocument)
{
   xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
   xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
}      
       }catch(e){}
5.根据不同的返回类型分别对返回的数据进行处理
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表