询问者
Ajax调用WCF服务问题

问题
-
最近在webcast上学习了前辈的WCF与Ajax开发实践系列课程,学到第三讲的时候。我下载demo,试着仿写一个Ajax调用WCF服务小例子,但一直出错,而且这个错误也是在我以前一直无法解决的。1.WCF Service代码:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; using System.Text; [ServiceContract(Namespace = "AjaxWcf")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class AjaxWCFServer { // 要使用 HTTP GET,请添加 [WebGet] 特性。(默认 ResponseFormat 为 WebMessageFormat.Json) // 要创建返回 XML 的操作, // 请添加 [WebGet(ResponseFormat=WebMessageFormat.Xml)], // 并在操作正文中包括以下行: // WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; [OperationContract] public void DoWork() { // 在此处添加操作实现 return; } [OperationContract] public string ReResult(string name) { return string.Format("123:{0}", name); } // 在此处添加更多操作并使用 [OperationContract] 标记它们 }
2.ASP.NET AJAX客户端<script type="text/javascript"> function WCFAjaxServer() { var name = $get("txtScript").value; var ajaxClient = new AjaxWcf.AjaxWCFServer(); // $post("WCFAjaxService.svc/ReResult" ajaxClient.ReResult(name, onSucceeded, onError); } function onSucceeded(reResult) { $get("txtResult").value = reResult.toString(); } function onError(reResult) { alert("出错了!"); } </script>
浏览器提示错误:行: 12
错误: 'AjaxWcf' 未定义调试错误提示:Microsoft JScript 运行时错误‘AjaxWCF’未定义------------配置文件,命名空间,引用都全看了,而且也在demo里面创建,还是不行。应该是Ajax调用时出错,但不知道错在哪里。弄了一个晚上,依然搞不定,为了多活两年,也是给前辈一个机会扶助国家栋梁。希望前辈百忙之中给我解决小问题....感激不尽。
全部回复
-
Hi,
从你上面列出的代码看,这些代码没有什么问题。你看下页面服务引用的路径是否正确.
例如:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="eDataService.svc" />
</Services>
</asp:ScriptManager>
Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework -
我猜测是你添加新项目的时候选择不对。
你重新添加一个WCF 服务类试验一下,
操作步骤如下图
这里与之前的ASP.NET Web Service不同,我们需要自己创建一个支持AJAX的WCF Service即可,如图所示:
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/