页面:
<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(/</g,"<").replace(/>/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>
(作者:李天平 转载请注明)