欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS使用post提交的兩種方式

 更新時間:2015年12月03日 10:50:33   作者:wiseideal  
這篇文章主要介紹了JS使用post提交的兩種方式,實(shí)例分析了兩種JavaScript使用post提交的實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實(shí)例講述了JS使用post提交的兩種方式。分享給大家供大家參考,具體如下:

第一種提交post的方式是傳統(tǒng)方式,判斷瀏覽器進(jìn)行post請求。

<SCRIPT stype=text/javascript>
var xmlobj; //定義XMLHttpRequest對象
function CreateXMLHttpRequest()
{
if(window.ActiveXObject)
//如果當(dāng)前瀏覽器支持Active Xobject,則創(chuàng)建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)
//如果當(dāng)前瀏覽器支持XMLHttp Request,則創(chuàng)建XMLHttpRequest對象
{
  xmlobj = new XMLHttpRequest();
}
}
function SubmitArticle(act,cityname,antique) //主程序函數(shù)
{
  CreateXMLHttpRequest(); //創(chuàng)建對象
  //var parm = "act=firstweather" ;//構(gòu)造URL參數(shù)
  //antique = escape(antique);
  var parm = "act=" + act + "&cityname=" + cityname + "&antique=" + antique;//構(gòu)造URL參數(shù)
  //xmlobj.open("POST", "{dede:global.cfg_templeturl/}/../include/weather.php", true); //調(diào)用weather.php
  xmlobj.open("POST", "/weather/include/weather.php", true); //調(diào)用weather.php
  xmlobj.setRequestHeader("cache-control","no-cache");
  xmlobj.setRequestHeader("contentType","text/html;charset=uft-8") //指定發(fā)送的編碼
  xmlobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");  //設(shè)置請求頭信息
  xmlobj.onreadystatechange = StatHandler;  //判斷URL調(diào)用的狀態(tài)值并處理
  xmlobj.send(parm); //設(shè)置為發(fā)送給服務(wù)器數(shù)據(jù)
}

第二種方式則是虛擬表單的形式提交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;
}

調(diào)用方法 如:

復(fù)制代碼 代碼如下:
post('pages/statisticsJsp/excel.action', {html :prnhtml,cm1:'sdsddsd',cm2:'haha'});

希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論