Js的兩種post方式

來源:互聯網
上載者:User

標籤:

第一種提交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方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.