关于调用webservice的心得(2)

前面讲了那么多关于webservice的东西.现在来看一个具体的例子

这里我选用了使用http来调用webservice的方法
我使用的webservie地址是
http://www.wopos.com/webservice/weather.asmx
其中有一个getWeather的函数。我们可以从
http://www.wopos.com/webservice/weather.asmx?op=getWeather
中看到其协议格式。因为使用http的方法调用所以应该按照http post规定的格式传给webservice数据。并且按照http get格式接受并且解析数据。
具体的实验代码如下

 


<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>New Page 1</title>
<style>
.WeatherReportTable
{
 border-collapse: collapse;
 font-family:Tahoma;
 font-size:8pt;
 color:#333333
}

 

</style>

<SCRIPT language="JavaScript">


function renderWeatherReport(data)
{
 dataItems = data.split(",");

 var tableHtml = '<table class ="WeatherReportTable" width="100%" border="0">';
 
 
 for(var i = 0; i < dataItems.length;i++)
 {
  columnContents = dataItems[i].split("=="); 
  //ignore the picture
  //you can add as you want
  if(columnContents [0] == '图1' || columnContents [0] == '图2')
  {
   continue;
  }
  var rowHtml = '<tr>';
  rowHtml += '<td>';
  rowHtml += columnContents[0] + '</td>';
  rowHtml += '<td>' + columnContents[1] + '</td>';
  rowHtml += '</tr>'
  tableHtml += rowHtml;
 }
 tableHtml += '</table>';
 console.innerHTML = tableHtml;
}
                        // Client invoke WebService use HTTP POST request and response
function GetWeatherReportHttpPOST()
{
 var URL = "http://www.wopos.com/webservice/weather.asmx/getWeather";
 var Params = "mCity=上海";// you can change your city name here,IN CHINESE ATTENTION!!!!
 var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.Open("POST",URL, false);
 xmlhttp.SetRequestHeader ("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
 xmlhttp.SetRequestHeader ("Content-Length",Params.length);
 xmlhttp.send(Params);
 var x =   xmlhttp.responseXml;
 //alert(x.text);
 if(xmlhttp.status == 200)
 {
  renderWeatherReport(x.text);
 }
 else
 {
  console.innerHTML = xmlhttp.statusText;
 }
}
                       
                      

window.onload = function()
{

 GetWeatherReportHttpPOST();
}

                        </SCRIPT>

</head>

<body>


<div id="console">

</div>

 

</body>

</html>

需要注意的是.xmlhttp是不能垮域调用的~~上面的代码在ie中直接打开文件(就是从资源管理其中打开文件)是成功的~可是如果放在虚拟目录中用localhost域调用就不行(除非webservice也是在localhost域的)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值