ajax 调用webservice的两种方式

本文介绍了一种通过JavaScript实现的SOAP协议调用远程服务的方法。具体来说,它演示了如何构造SOAP请求并发送到指定的Web服务接口以获取特定城市的天气信息。

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312"/>
<title>test</title>

</head>
<body>
<input value="send" type="button" style="width: 61px" id="Button1" name="Button1" onclick="return Button1_onclick()" />
<div id="result"></div>
<script language="javascript" type="text/javascript">
// <!CDATA[
var xmlhttp

 var r=document.getElementById("result");
 
function Button1_onclick() {
SoapProtocal();

}
function HttpPost()
{
 xmlhttp=getHTTPObject();
xmlhttp.open("post","http://fhs.6617.com/getweather.asmx/GetCityWeather",true);
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlhttp.onreadystatechange=showresult;
xmlhttp.send("city=北京");
}

function SoapProtocal()
{
 xmlhttp=getHTTPObject();
xmlhttp.open("post","http://fhs.6617.com/getweather.asmx",true);
xmlhttp.setRequestHeader('Content-Type','text/xml; charset=utf-8');
 xmlhttp.setRequestHeader('SOAPAction','http://fhs.6617.com/WeatherService/GetCityWeather');
xmlhttp.onreadystatechange=showresult;
var tt;
tt='<?xml version="1.0" encoding="utf-8"?>';
tt+='<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
tt+='<soap:Body>';
tt+="<GetCityWeather xmlns=/"http://fhs.6617.com/WeatherService/">"
tt+="<city>北京</city>"
tt+="</GetCityWeather>";
tt+='</soap:Body>';
tt+='</soap:Envelope>';
xmlhttp.send(tt);
}

 


function showresult(){

  if (xmlhttp.readyState == 4)
  {
   if (xmlhttp.status == 200)
   {
      var result = xmlhttp.responseText;
     r.innerHTML =r.innerHTML+ result +"<br/>"  ;  
     
   }
   else
    r.innerHTML  =r.innerHTML+xmlhttp.status  +"<br/>"  ;     
  }
}

 

function getHTTPObject(){
  var xmlhttp = false;
   if(window.XMLHttpRequest){
  xmlhttp = new XMLHttpRequest();
  if(xmlhttp.overrideMimeType){
   xmlhttp.overrideMimeType('text/xml');
  }
   }
   else{
  try{
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }catch(e){
   try{
 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }catch(E){
  xmlhttp = false;
  }
 }
   }
  return xmlhttp;
  }


// ]]>
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值