js調用WebService

來源:互聯網
上載者:User
js|web

<!--
    //使用方法,直接調用sendRequest(url, Method, HttpMethod, params)方法
    //參數說明:url--訪問地址;Method--調用的服務方法;HttpMethod--傳遞方法,預設post;params--需要傳遞的參數或資訊;
    //多參數使用&串連,post方法經過測試;
    //注意!
    //成功傳輸迴響函數為xmlResponse(),請自行在此js引用之前添加,否則無效,迴響內容為req.responseText,預設為同步。
  function getXMLRequester( ){ //此函數是建立XMLHTTP組件的,可能ie低版本無法使用,請參閱msdn;
  var xmlhttp_request = false;
  try{
  if( window.ActiveXObject ){
  for( var i = 5; i; i-- ){
  try{
  if( i == 2 ){
  xmlhttp_request = new ActiveXObject( "Microsoft.XMLHTTP" );
  }else{
  xmlhttp_request = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
 xmlhttp_request.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
  }
  break;}
  catch(e){
  xmlhttp_request = false;
  }
  }
  }else if( window.XMLHttpRequest ){
  xmlhttp_request = new XMLHttpRequest();
  if (xmlhttp_request.overrideMimeType) {
  xmlhttp_request.overrideMimeType('text/xml');
  }
  }
  }catch(e){
  xmlhttp_request = false;
 alert("對不起您的瀏覽器版本太低,請更新後使用。");
  }
  return xmlhttp_request ;
  }

var req;
function sendRequest(url, Method, HttpMethod, params) {
 url += "/" + Method;
  if (!HttpMethod){
    HttpMethod = "POST"; 
  } 
  req = getXMLRequester(); //建立組件
  if (req) {
    req.onreadystatechange = processReqChange;//調用進程監視函數
    req.open(HttpMethod, url, true);
 req.setRequestHeader("Host", "202.115.138.116");
 req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 req.setRequestHeader("Content-Length",params.length);
 req.send(params); //發送資料參數
  }
}

function processReqChange() {
    // 監視資料傳遞。
    if (req.readyState == 4) {
        if (req.status == 200) {
           xmlResponse();        // connect OK 執行輸出函數out()
         } else { //拋出錯誤
            alert("無法正常串連伺服器,錯誤:\n" +
                req.statusText+":"+req.status);
         }
    }
}
-->
<HTML>
<HEAD>
<META http-equiv='Content-Type' content='text/html;charset=gb2312'>
<Meta name='Gemeratpr' content='網路程式員伴侶(Lshdic)2005'>
<TITLE></TITLE>
<SCRIPT language='Jscript'>
<!--
function xmlResponse() {
 message.value = req.responseText;
}
-->
</SCRIPT>
<SCRIPT language=jscript src='file:///C:/Documents and Settings/losingrose/案頭/js/HttpRequest.js' ></SCRIPT>
</HEAD>
<BODY>
<textarea id='send' value='' rows='2' style='width:100;' ></textarea>
<INPUT type='button' value='sdfsdf' onclick='sendRequest("http://202.115.138.116/ChatWebService/Service.asmx","dataset","POST","a="+send.value+"&b=2&c=3453");' />
<textarea id='message' value='' rows='8' style='width:500;' ></textarea>
</BODY>
</HTML>

 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.