📄 106_2.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<base href="http://localhost:8080/" />
<title>AJAX调用WCF服务项模板[ASP.NET]</title>
<script language="javascript">
var sPath = document.location.protocol + "//" + document.location.host + document.location.pathname;
if(sPath.indexOf("/article/")>=0)
{
var sPath_tmp = sPath.split("/article/");
var BasePath = sPath_tmp[0]+"/";
document.write('<base href="'+BasePath+'" />');
}
</script>
<script language="javascript" src="images/js.js"></script>
<link href="css/css2.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="950" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="3" bgcolor="#106B61"></td>
</tr>
<tr>
<td><table width="950" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="200" height="87" align="center" background="images/index5_02.gif"><img src="images/logo.gif" width="142" height="62" /></td>
<td valign="bottom"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="40"><table width="99%" border="0" align="left" cellpadding="0" cellspacing="0">
<tr>
<td width="10%" align="left" class="red12"><strong>滚动新闻:</strong></td>
<td width="90%" align="left">
<div id="pro_demo" style="overflow:hidden;height:40px;width:660px;">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td id="pro_demo1" height="40"><table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td class="gray" nowrap="nowrap"> ·<a href="article/99.html" title="2.0版本发布">2.0版本发布</a></td>
<td class="gray" nowrap="nowrap"> ·<a href="article/89.html" title="2009-4-28更新日志">2009-4-28更新日志</a></td>
<td class="gray" nowrap="nowrap"> ·<a href="article/88.html" title="新闻系统全站静态化测试中">新闻系统全站静态化测试中</a></td>
<td class="gray" nowrap="nowrap"> ·<a href="article/83.html" title="Ok3w ASP新闻发布系统功能简介">Ok3w ASP新闻发布系统功能简介</a></td>
<td class="gray" nowrap="nowrap"> ·<a href="article/82.html" title="Ok3w ASP新闻发布系统功能简介">Ok3w ASP新闻发布系统功能简介</a></td>
<td class="gray" nowrap="nowrap"> ·<a href="article/81.html" title="Ok3w ASP新闻发布系统功能简介">Ok3w ASP新闻发布系统功能简介</a></td>
<td class="gray" nowrap="nowrap"> ·<a href="article/46.html" title="Ok3w ASP新闻发布系统功能简介">Ok3w ASP新闻发布系统功能简介</a></td>
</tr>
</table></td>
<td id="pro_demo2"></td>
</tr>
</table>
<script language="javascript" src="images/marquee.js"></script>
</div> </td>
</tr>
</table></td>
</tr>
<tr>
<td bgcolor="#106B61"><table border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left"><img src="images/index5_08.gif" width="5" height="41" /></td>
<td align="center"> <a href="" class="white14b">网站首页</a> </td>
<td align="center"><img src="images/index5_11.gif" width="2" height="25" /></td>
<td align="center"> <a href="article/list_95_1.html" class="white14b">新闻资讯</a> </td>
<td align="center"><img src="images/index5_11.gif" width="2" height="25" /></td>
<td align="center"> <a href="article/list_102_1.html" class="white14b">网络安全</a> </td>
<td align="center"><img src="images/index5_11.gif" width="2" height="25" /></td>
<td align="center"> <a href="article/list_107_1.html" class="white14b">网页制作</a> </td>
<td align="center"><img src="images/index5_11.gif" width="2" height="25" /></td>
<td align="center"> <a href="article/list_85_1.html" class="white14b">办公软件</a> </td>
<td align="center"><img src="images/index5_11.gif" width="2" height="25" /></td>
<td align="center"> <a href="article/list_84_1.html" class="white14b">网站运营</a> </td>
<td align="center"><img src="images/index5_11.gif" width="2" height="25" /></td>
<td align="center"> <a href="article/list_83_1.html" class="white14b">平面设计</a> </td>
<td align="center"><img src="images/index5_11.gif" width="2" height="25" /></td>
<td align="center"> <a href="article/list_60_1.html" class="white14b">网络编程</a> </td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td height="37" align="center" background="images/index5_15.gif"><table width="98%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" class="green12gray">今天是<script language="javascript">
var mydate=new Date();
var mymonth=mydate.getMonth()+1;
var myday= mydate.getDate();
var myyear= mydate.getYear();
document.write(myyear+"-"+mymonth+"-"+myday);</script>,欢迎访问Ok3w新闻发布系统!</td>
<td align="right" class="green12gray"><a href="guest.asp">请您留言</a> | <a href="http://www.glzy8.com/ok3w/article/147.html" target="_blank">免费下载</a> | <a href="#" class="green12gray" onclick="this.style.behavior='url(#default#homepage)';this.setHomePage(document.URL);">设为首页</a> | <a href="#" class="green12gray" onclick="window.external.AddFavorite(document.URL,document.title);">加入收藏</a></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0" style="margin-top:2px;">
<tr>
<td align="left"><iframe id="baiduSpFrame" border="0" vspace="0" hspace="0" marginwidth="0" marginheight="0" framespacing="0" frameborder="0" scrolling="no" width="468" height="60" src="http://spcode.baidu.com/spcode/spstyle/style2220.jsp?tn=zhengbi_sp&ctn=0&styleid=2220"></iframe></td>
<td align="right"><iframe id="baiduSpFrame" border="0" vspace="0" hspace="0" marginwidth="0" marginheight="0" framespacing="0" frameborder="0" scrolling="no" width="468" height="60" src="http://spcode.baidu.com/spcode/spstyle/style1634.jsp?tn=zhengbi_sp&ctn=0&styleid=1634"></iframe></td>
</tr>
<tr>
<td colspan="2" align="left" height="5"></td>
</tr>
<tr>
<td align="left"><iframe id="baiduSpFrame" border="0" vspace="0" hspace="0" marginwidth="0" marginheight="0" framespacing="0" frameborder="0" scrolling="no" width="468" height="60" src="http://spcode.baidu.com/spcode/spstyle/style2615.jsp?tn=zhengbi_sp&ctn=0&styleid=2615"></iframe></td>
<td align="right"><iframe id="baiduSpFrame" border="0" vspace="0" hspace="0" marginwidth="0" marginheight="0" framespacing="0" frameborder="0" scrolling="no" width="468" height="60" src="http://spcode.baidu.com/spcode/spstyle/style2475.jsp?tn=zhengbi_sp&ctn=0&styleid=2475"></iframe></td>
</tr>
</table></td>
</tr>
<tr>
<td height="14" background="images/index5_31.gif"></td>
</tr>
</table>
<table width="950" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="40">您当前位置:<a href="">网站首页</a> >> <a href="article/list_107_1.html">网页制作</a> >> <a href="article/list_132_1.html">Javascript/Ajax</a> >> 阅读文章</td>
<form id="form1" name="form1" method="get" action="search.asp">
<td width="344"><span style="font-weight: bold">站内查找:</span>
<input name="keyword" type="text" id="keyword" size="29" />
<input type="submit" name="Submit" value="搜索" />
</td>
</form>
</tr>
</table>
<table width="950" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="left" valign="top"><table width="100%" border="0" cellpadding="8" cellspacing="0" style="border:1px solid #AEE1DC;">
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0" style="table-layout: fixed;">
<tr>
<td align="center"><h1>AJAX调用WCF服务项模板[ASP.NET]</h1>
<hr size="1" color="#EBEBEB" />
来源:IT168 发布时间:2009-05-01 00:35:24 查看次数:<span id="News_Hits"></span><iframe style="display:none;" src="hits.asp?type=news&id=106"></iframe>
<hr size="1" color="#EBEBEB" />
</td>
</tr>
<tr>
<td style="word-break: break-all; word-wrap:break-word;"><div class="gray14">如果你打开后台代码文件,将看到如下形式的代码:</P><PRE><OL class=dp-c><LI class=alt><SPAN><SPAN class=keyword>namespace</SPAN><SPAN> WebUI </SPAN></SPAN><LI><SPAN>{ </SPAN><LI class=alt><SPAN> [ServiceContract(Namespace = </SPAN><SPAN class=string>""</SPAN><SPAN>)] </SPAN><LI><SPAN> [AspNetCompatibilityRequirements(RequirementsMode = </SPAN><LI class=alt><SPAN> AspNetCompatibilityRequirementsMode.Allowed)] </SPAN><LI><SPAN> </SPAN><SPAN class=keyword>public</SPAN><SPAN> </SPAN><SPAN class=keyword>class</SPAN><SPAN> HelloWorldService </SPAN><LI class=alt><SPAN> { </SPAN><LI><SPAN> [OperationContract] </SPAN><LI class=alt><SPAN> </SPAN><SPAN class=keyword>public</SPAN><SPAN> </SPAN><SPAN class=keyword>void</SPAN><SPAN> DoWork() </SPAN><LI><SPAN> { </SPAN><LI class=alt><SPAN> </SPAN><SPAN class=keyword>return</SPAN><SPAN>; </SPAN><LI><SPAN> } </SPAN><LI class=alt><SPAN> } </SPAN><LI><SPAN>} </SPAN></LI></OL></PRE>
<P>VS 2008会自动为你在web.config文件中添加一些必要的配置,因此不需要手工在web.config文件中添加任何配置。现在,继续并添加一个HelloWorld()方法,该方法返回一个“HelloWorld”的字符串并为该方法添加一个[OperationContract]特性。在本篇文章的后面我们将解释该特性。</P>
<P>现在,为Web应用程序工程添加一个页面并命名它为“HelloWorldTest.aspx”。从Visual Studio工具箱中拖拽一个Script Manager项到页面上。在ScriptManager 标签内部,添加一个到该服务的引用。下面给出了该代码示例:</P><PRE><OL class=dp-xml><LI class=alt><SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>asp:ScriptManager</SPAN><SPAN> </SPAN><SPAN class=attribute>ID</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"ScriptManager1"</SPAN><SPAN> </SPAN><SPAN class=attribute>runat</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"server"</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN></SPAN><LI><SPAN> </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>Services</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>asp:ServiceReference</SPAN><SPAN> </SPAN><SPAN class=attribute>Path</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"~/HelloWorldService.svc"</SPAN><SPAN> </SPAN><SPAN class=tag>/></SPAN><SPAN> </SPAN><LI><SPAN> </SPAN><SPAN class=tag></< SPAN><SPAN class=tag-name>Services</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN><LI class=alt><SPAN class=tag></< SPAN><SPAN class=tag-name>asp:ScriptManager</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN></LI></OL></PRE>
<P>现在,添加一个button按钮和一个textbox文本框到页面上,并为该按钮添加点击事件处理程序,以使用JavaScript函数来调用服务。当你编写服务调用函数时,Visual Studio 2008 HTML编辑器将提供智能感知功能以方便编写必要的函数调用。完整的HTML代码如下所示:</P><PRE><OL class=dp-xml><LI class=alt><SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>form</SPAN><SPAN> </SPAN><SPAN class=attribute>id</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"form1"</SPAN><SPAN> </SPAN><SPAN class=attribute>runat</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"server"</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN></SPAN><LI><SPAN class=tag><</SPAN><SPAN class=tag-name>div</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>script</SPAN><SPAN> </SPAN><SPAN class=attribute>language</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"javascript"</SPAN><SPAN> </SPAN><SPAN class=attribute>type</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"text/javascript"</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN><LI><SPAN> function GetValueFromServer() { </SPAN><LI class=alt><SPAN> HelloWorldService.HelloWorld(onSuccess, onFailure); </SPAN><LI><SPAN> } </SPAN><LI class=alt><SPAN> function onSuccess(result) { </SPAN><LI><SPAN> document.getElementById('txtValueContainer')</SPAN><SPAN class=attribute>.value</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>result</SPAN><SPAN>; </SPAN><LI class=alt><SPAN> } </SPAN><LI><SPAN> </SPAN><LI class=alt><SPAN> function onFailure(result) { </SPAN><LI><SPAN> window.alert(result); </SPAN><LI class=alt><SPAN> } </SPAN><LI><SPAN> </SPAN><SPAN class=tag></< SPAN><SPAN class=tag-name>script</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>asp:ScriptManager</SPAN><SPAN> </SPAN><SPAN class=attribute>ID</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"ScriptManager1"</SPAN><SPAN> </SPAN><SPAN class=attribute>runat</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"server"</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN><LI><SPAN> </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>Services</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>asp:ServiceReference</SPAN><SPAN> </SPAN><SPAN class=attribute>Path</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"~/HelloWorldService.svc"</SPAN><SPAN> </SPAN><SPAN class=tag>/></SPAN><SPAN> </SPAN><LI><SPAN> </SPAN><SPAN class=tag></< SPAN><SPAN class=tag-name>Services</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><SPAN class=tag></< SPAN><SPAN class=tag-name>asp:ScriptManager</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN><LI><SPAN> </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>input</SPAN><SPAN> </SPAN><SPAN class=attribute>id</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"btnServiceCaller"</SPAN><SPAN> </SPAN><SPAN class=attribute>type</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"button"</SPAN><SPAN> </SPAN><SPAN class=attribute>value</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"Get Value"</SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><SPAN class=attribute>onclick</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"GetValueFromServer()"</SPAN><SPAN>; </SPAN><SPAN class=tag>/></SPAN><SPAN> </SPAN><LI><SPAN> </SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>input</SPAN><SPAN> </SPAN><SPAN class=attribute>id</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"txtValueContainer"</SPAN><SPAN> </SPAN><SPAN class=attribute>type</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"text"</SPAN><SPAN> </SPAN><SPAN class=attribute>value</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>""</SPAN><SPAN> </SPAN><SPAN class=tag>/></SPAN><SPAN> </SPAN><LI class=alt><SPAN class=tag></< SPAN><SPAN class=tag-name>div</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN><LI><SPAN class=tag></< SPAN><SPAN class=tag-name>form</SPAN><SPAN class=tag>></SPAN><SPAN> </SPAN></LI></OL></PRE>
<P>注意,当调用服务时,我们传递了两个方法:一个用于进行回调而另一个用于错误回调。如果我们需要传递任何参数到该函数中,这些参数会首先传递进去然后再进行回调。</P>
<P>因此,如果我们有一个名为getvalue 的函数并接收两个字符串参数,那么我们将以[NameSpaceName].[ServiceName].getvalue(“value one”,”value two”,on_success,on_error);的方式调用该函数,此刻on_success和on_error各自表示回调和错误回调。</P></SPAN></SPAN></SPAN></SPAN></SPAN></SPAN></SPAN><div class="thisPageNav"><a href="article/106.html">第1页</a> <a href="article/106_2.html"><strong>第2页</strong></a> </div></div></td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -