下面的文章将介绍:
how Microsoft “Atlas” helps web developers make network callbacks directly from client-side script code.
本文将学习以下内容:
- how to create a simple web service that is “Atlas” enabled;
- how to call this web service from client-side JavaScript code;
- how to use the data returned from the web service.
详细步骤:
1、先见一个简单的web service,只将HelloWorld方法小改如下:
[WebMethod]
public string HelloWorld(String ss)
{
return "Hello " + ss;
}
2、 在“源”页面里,把代码“<atlas:ScriptManager ID="ScriptManager1" runat="server" / >”修改成如下代码:
<atlas:ScriptManager ID="ScriptManager1" runat="server" >
<Services>
<atlas:ServiceReference Path="WebService.asmx" />
</Services>
</atlas:ScriptManager>
3、在“设计”页面里从“ HTML ” 选项卡里拖拽一个“text”和“button”控件,双击“button”控件,切换到“源”页面,把代码“function Button1_onclick() { }”修改成如下代码:
function Button1_onclick()
{
abc = WebService.HelloWorld(document.getElementById('Text1').value,OnComplete,OnTimeOut);
return false;
}
function OnComplete(result)
{
alert(result);
}
function OnTimeOut(result)
{
alert("超时!");
}
4、保存所有文件, F5运行一把,在“text”里输入任意字符,点击按钮,检验一下效果。
完整代码:
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > Untitled Page </ title >
< script language ="javascript" type ="text/javascript" > ...
// <!CDATA[
function Button1_onclick()
...{
abc = WebService.HelloWorld(document.getElementById('Text1').value,OnComplete,OnTimeOut);
return false;
}
function OnComplete(result)
...{
alert(result);
}
function OnTimeOut(result)
...{
alert("超时!");
}
// ]]>
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >
< atlas:ScriptManager ID ="ScriptManager1" runat ="server" >
< Services >
< atlas:ServiceReference Path ="WebService.asmx" />
</ Services >
</ atlas:ScriptManager >
< br />
请输入你的姓名:
< input id ="Text1" type ="text" />
< input id ="Button1" type ="button" value ="button" onclick ="return Button1_onclick()" /></ form >
</ body >
</ html >