wangchangtao 发表于 2013-1-23 01:19:29

ajax方式提交form

思路:
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.根据不同的返回类型分别对返回的数据进行处理
页: [1]
查看完整版本: ajax方式提交form