js调用WebService

博客介绍了使用XMLHttpRequest发送请求的方法,包括建立XMLHTTP组件的函数getXMLRequester,以及发送请求的函数sendRequest,还说明了请求状态监视函数processReqChange和回响函数xmlResponse的使用,给出了示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!--
    //使用方法,直接调用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>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值