標籤:
第一種提交post的方式是傳統方式,判斷瀏覽器進行post請求。
var xmlobj; //定義XMLHttpRequest對象 function CreateXMLHttpRequest() { if(window.ActiveXObject) //如果當前瀏覽器支援Active Xobject,則建立ActiveXObject對象 { //xmlobj = new ActiveXObject("Microsoft.XMLHTTP"); try { xmlobj = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlobj = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlobj = false; } } } else if(window.XMLHttpRequest) //如果當前瀏覽器支援XMLHttp Request,則建立XMLHttpRequest對象 { xmlobj = new XMLHttpRequest(); } } function SubmitArticle(act,cityname,antique) //主程式函數 { CreateXMLHttpRequest(); //建立對象 //var parm = "act=firstweather" ;//構造URL參數 //antique = escape(antique); var parm = "act=" + act + "&cityname=" + cityname + "&antique=" + antique;//構造URL參數 //xmlobj.open("POST", "{dede:global.cfg_templeturl/}/../include/weather.php", true); //調用weather.php xmlobj.open("POST", "/weather/include/weather.php", true); //調用weather.php xmlobj.setRequestHeader("cache-control","no-cache"); xmlobj.setRequestHeader("contentType","text/html;charset=uft-8") //指定發送的編碼 xmlobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); //佈建要求頭資訊 xmlobj.onreadystatechange = StatHandler; //判斷URL調用的狀態值並處理 xmlobj.send(parm); //設定為發送給伺服器資料 }
第二種方式則是虛擬表單的形式提交post請求
function post(URL, PARAMS) { var temp = document.createElement("form"); temp.action = URL; temp.method = "post"; temp.style.display = "none"; for (var x in PARAMS) { var opt = document.createElement("textarea"); opt.name = x; opt.value = PARAMS[x]; // alert(opt.name) temp.appendChild(opt); } document.body.appendChild(temp); temp.submit(); return temp; } 調用方法 如 post(‘pages/statisticsJsp/excel.action‘, {html :prnhtml,cm1:‘sdsddsd‘,cm2:‘haha‘});
轉載自:http://wiseideal.iteye.com/blog/1582947
Js的兩種post方式