十月 27, 2008

Js 调用 WebService心得总结

Written by
 
页面: 
  <INPUT type=”button” value=”Button” onclick=”getdata()”><br>
  <span id=”div1″ class=”list”></span>
   <script language=javascript>
     setInterval(getdata,1000); //动态定时调用
   </script>
脚本:
   <SCRIPT ID=”clientEventHandlersJS” LANGUAGE=”javascript”>
  function getdata()
  {
    var index=0;   
    docSubmit =new ActiveXObject(“Microsoft.XMLDOM”);
    docSubmit.async = false;   
    docSubmit.load(“http://localhost/WebApplication1/Service1.asmx/GetData?maxval=5”);   
    docSubmit.loadXML(docSubmit.xml.replace(/&lt;/g,”<“).replace(/&gt;/g,”>”));
       
    var s=””;  
    nodeList=docSubmit.documentElement.getElementsByTagName(“ds”);
    for (i=0;i<nodeList.length;i++)
    {
     s=s+nodeList(i).selectSingleNode(“KeShimch”).text+'<br>’;
    }
    div1.innerHtml =””;
    div1.innerHTML=s;
    div1.style.visibility=”visible”;       
  }   
  </script>
 
//如果返回是个字符串:
   <SCRIPT ID=”clientEventHandlersJS” LANGUAGE=”javascript”>
   function chang()
   {         
    docSubmit =new ActiveXObject(“Microsoft.XMLDOM”);
    docSubmit.async = false;  
    docSubmit.load(“http://localhost/Advertisement/ADService.asmx/GetADCode?ADID=1”);  
        
    var s=docSubmit.documentElement.text;  
    
    window.alert(s);    
    td_AD.innerHTML=s;        
   }   
   //setInterval(chang,2000);//间隔执行      
  </script>
 
注意:
在fromework1.0和1.1是有区别的,
在1.1的 machine.config (C:\WINNT\Microsoft.NET\Framework\v1.1.4322\CONFIG),默认webservice去除了post,get方法
你可在你的 machine.config 中加上 或者 只在 webServices 项目的 Web.config 的<system.web>里加上:
<webServices>
     <protocols>
       <add name=”HttpPost” />
       <add name=”HttpGet” />
     </protocols>
</webServices>
否则,在ie地址栏输入 webServices 访问地址串时,提示:请求格式无法识别。
还有
docSubmit = new ActiveXObject(“MSXML2.DOMDocument”);  //1.0
换成
docSubmit =new ActiveXObject(“Microsoft.XMLDOM”);  //1.1
--------------------------
web服务方法:
  [WebMethod]
  public string GetData(string maxval)
  {   
   BaseClass.DbManagerSql dbm=new BaseClass.DbManagerSql();
   string strSql=”select * from s_KeShi where Keshidm>”+maxval;
   DataSet ds=dbm.Query(strSql);
   return ds.GetXml();
  }
返回的xml数据:
  <?xml version=”1.0″ encoding=”utf-8″ ?>
  <string xmlns=”http://tempuri.org/”>
<NewDataSet>
<ds> <KeShidm>6</KeShidm> <KeShimch>财务部</KeShimch> </ds>
<ds> <KeShidm>7</KeShidm> <KeShimch>政工部</KeShimch> </ds>
<ds> <KeShidm>8</KeShidm> <KeShimch>安质部</KeShimch> </ds>
<ds> <KeShidm>9</KeShidm> <KeShimch>电管部</KeShimch> </ds>
<ds> <KeShidm>10</KeShidm> <KeShimch>运输公司</KeShimch> </ds>
<ds> <KeShidm>11</KeShidm> <KeShimch>局长</KeShimch> </ds>
<ds> <KeShidm>202</KeShidm> <KeShimch>变电站</KeShimch></ds>
</NewDataSet>
</string>

=========================================================

本文为转载,转载必须确保本文完整并完整保留原作者信息和本文链接

E-mail: khler@163.com

QQ:     23381103

MSN:   pragmac@hotmail.com

原文:http://www.webasp.net/article/28/27619.htm

=========================================================

 

Category : Web开发

Tags :

Comments

One Response

  1. 匿名说道:

    那个家让它沉下去吧!不要啦

发表评论

电子邮件地址不会被公开。

Proudly powered by WordPress and Sweet Tech Theme