js调用web service,为何不能刷新数据?

叶子哟 自由  2004-12-12 01:58:02
很简单的一个网页,用js自动更新,只能更新两遍,再也不能刷新,但点button可以:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div id="divWebServiceResults"></div>
<INPUT style="Z-INDEX: 101; LEFT: 280px; POSITION: absolute; TOP: 176px" type="button"
value="Button" onclick="getWebServiceResultsAfterInit();" >
</form>
<script language="javascript">
getWebServiceResultsAfterInit();
setTimeout(getWebServiceResultsAfterInit,1000); //********只能执行一遍,为何?******

var objXmlDoc;
var objHttp;
var bHttp = false;
function getWebServiceResultsAfterInit() {
if(!bHttp)
{
var aszHttpProgIDs = [ "MSXML2.XMLHTTP.4.0",

"MSXML2.XMLHTTP.3.0",

"MSXML2.XMLHTTP",

"Microsoft.XMLHTTP" ];

for (var i = 0; !bHttp && i < aszHttpProgIDs.length; i++) {

try {

objHttp = new ActiveXObject(aszHttpProgIDs[i]);

bHttp = true;

} catch (objException) {

}

}
}
if (!bHttp) {
return;
}

strEnvelope = "<soap:Envelope xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xsd=\"http://www.w3.org/2001/XMLSchema\"" +
" soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
" <soap:Body>" +
" </soap:Body>" +

"</soap:Envelope>"

objHttp.open("post", "http://localhost/WebService1/Service1.asmx/HelloWorld",false);
objHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
objHttp.setRequestHeader("SOAPAction", "http://localhost/WebService1/Service1.asmx/HelloWorld");
objHttp.send(strEnvelope);
var szResponse = objHttp.responseText;
divWebServiceResults.innerHTML = szResponse;
}

</script>
</body>
</HTML>
...全文
134 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
叶子哟 2004-12-12
晕了,呵呵,确实如litp所说,setTimeout只执行一次,应该用setInterval,呵呵!好时间没有用js了,差不多都忘了!
多谢各位了
回复
saucer 2004-12-12

litp(天道酬勤) 是对的,你需要用

setInterval(getWebServiceResultsAfterInit,2000);
回复
saucer 2004-12-12
我上面的好象是答非所问,
1. 把Timeout时间加大
setTimeout(getWebServiceResultsAfterInit,1000);
===>
setTimeout(getWebServiceResultsAfterInit,2000);

2. 看是否有错
function getWebServiceResultsAfterInit() {
try
{
....
}
catch(e)
{
alert(e.description);
}
}
回复
李天平 2004-12-12
Js 调用 WebService


页面:
<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>

注意:
我试了一下,问题在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();
}
回复
saucer 2004-12-12
恐怕是缓存问题,试试

var dt = new Date();
var s = escape(dt.ToString());
objHttp.open("post", "http://localhost/WebService1/Service1.asmx/HelloWorld?"+s,false);

还有,别这么调用WebService, 用WebService Behavior
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/webservice/overview.asp
回复
相关推荐
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2004-12-12 01:58
社区公告
暂无公告