下載用ajax很差使,得用表單提交的方式ajax
download:function(url,paramObj){ var doc = document; //使用一個隱藏的form表單執行提交,沒有則建立 var downloadForm = doc.getElementById('_downloadForm'); if(!downloadForm){ downloadForm = doc.createElement('form'); downloadForm.id = '_downloadForm'; doc.body.appendChild(downloadForm);//必須加入body中,不然可能不下載 } downloadForm.method = 'post'; downloadForm.action = url; downloadForm.style.display='none';//隱藏 downloadForm.target='_blank'; //移除以前表單內的全部域 while(downloadForm.hasChildNodes()){ downloadForm.removeChild(downloadForm.firstChild); } //根據傳入的參數,建立表單隱藏域 if(paramObj != null){ var hidden; for(var key in paramObj){ if(paramObj[key]==null || paramObj[key] == undefined){ continue; } hidden = doc.createElement('input'); hidden.type='hidden'; hidden.name = key; hidden.value = paramObj[key]; downloadForm.appendChild(hidden); } } downloadForm.submit();//提交,執行下載 }