JS调用web service的通用函数

调用:
RequestByPost(method,variable,value,url,_Namespace)

method:web service的方法名
variable:web service的方法的变量名数组
value:web service的方法的变量的值的数组
url:请求的地址(asmx文件地址)
_Namespace:web service的命名空间

  1. <script language="javascript" type="text/javascript">
  2. // <!CDATA[
  3. //define
  4. var xmlhttp;
  5. var value=new Array();
  6. var variable=new Array();
  7. //Show Response MSG.
  8. function handleStateChange()
  9. {
  10.     var h=document.getElementById("Label1");
  11.     if(xmlhttp.readyState==4)
  12.     {
  13.         if(xmlhttp.status==200)
  14.         {
  15.             alert(xmlhttp.responseText);
  16.             h.innerHTML=xmlhttp.responseText;
  17.             //h.innerHTML=xmlhttp.responseXML;
  18.         }
  19.         else if(xmlhttp.status==404)
  20.         {
  21.             h.innerHTML="<br>找不到请求的服务器资源!";
  22.         }
  23.     }
  24.     else if(xmlhttp.readyState==0)
  25.     {
  26.         h.innerHTML="<br>未初始化!";
  27.     }
  28.     else if(xmlhttp.readyState==1)
  29.     {
  30.         h.innerHTML="<br>正在加载……!";
  31.     }
  32.     else if(xmlhttp.readyState==2)
  33.     {
  34.         h.innerHTML="<br>已经加载完成!";
  35.     }
  36.     else if(xmlhttp.readyState==3)
  37.     {
  38.         h.innerHTML="<br>正在和服务器交互";
  39.     }
  40.     else
  41.     {
  42.         h.innerHTML=xmlhttp.responseXML;
  43.     }
  44.     
  45. }
  46. //Get Request Data's length
  47. function getlen(str)
  48. {
  49. var bytesCount=0;
  50. for (var i = 0; i < str.length; i++)
  51. {
  52. var c = str.charAt(i);
  53. if (/^[\u0000-\u00ff]$/.test(c))  //匹配双字节
  54.       {
  55. bytesCount += 1;
  56. }
  57. else
  58. {
  59. bytesCount += 2;
  60. }
  61. }
  62. return bytesCount;
  63. }  
  64.     
  65. //Create XMLHttpRequest Object
  66.   function createXMLHttpRequest()
  67. {
  68.     if(window.ActiveXObject)
  69.     {
  70.         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  71.     }
  72.     else if(window.XMLHttpRequst)
  73.     {
  74.         xmlhttp=new XMLHttpRequest();
  75.     }
  76. }  
  77. //send Request By HTTP POST
  78. function RequestByPost(method,variable,value,url,_Namespace)
  79. {
  80. createXMLHttpRequest();
  81. var data;
  82. data = '<?xml version="1.0" encoding="utf-8"?>'; 
  83. data = data + '<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/">'; 
  84. data = data + '<soap:Body>'; 
  85. data = data + '<'+method+' xmlns="'+_Namespace+'">'; 
  86. for(var i=0;i<variable.length;i++)
  87. {
  88.     data = data + '<'+variable[i]+'>'+value[i]+'</'+variable[i]+'>'; 
  89. }
  90. data = data + '</'+method+'>'; 
  91. data = data + '</soap:Body>'; 
  92. data = data + '</soap:Envelope>'; 
  93. xmlhttp.onreadystatechange=handleStateChange;
  94. xmlhttp.Open("POST",url, true); 
  95. xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8"); 
  96. xmlhttp.SetRequestHeader ("Content-Length",getlen(data)); 
  97. xmlhttp.SetRequestHeader ("SOAPAction",_Namespace+method); 
  98. xmlhttp.Send(data); 
  99. alert(data);
  100. }

  101. //CallHelloWorld!
  102. function SayHello_onclick() {
  103. //alert(document.getElementById('YourName').value);
  104.   RequestByPost("HelloWorld",new Array("msg"),new Array(document.getElementById('YourName').value),"WebService.asmx","localhost/");
  105. }
  106. //WeatherReport Test:
  107. function Button2_onclick() {
  108. RequestByPost("getWeatherbyCityName",new Array("theCityName"),new Array(document.getElementById('CityName').value),"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx","http://WebXml.com.cn/");
  109. }
  110. // ]]>
  111. </script>
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值