首页 新闻 会员 周边

Ajax调用WCF服务问题

0
悬赏园豆:30 [已解决问题] 解决于 2012-04-02 20:59
最近在webcast上学习了前辈的WCF与Ajax开发实践系列课程,学到第三讲的时候。
我下载demo,试着仿写一个Ajax调用WCF服务小例子,但一直出错,而且这个错误也是在我以前一直无法解决的。
1.WCF Service代码:
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Runtime.Serialization;
5 using System.ServiceModel;
6 using System.ServiceModel.Activation;
7 using System.ServiceModel.Web;
8 using System.Text;
9
10 [ServiceContract(Namespace = "AjaxWcf")]
11 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
12 public class AjaxWCFServer
13 {
14 // 要使用 HTTP GET,请添加 [WebGet] 特性。(默认 ResponseFormat 为 WebMessageFormat.Json)
15 // 要创建返回 XML 的操作,
16 // 请添加 [WebGet(ResponseFormat=WebMessageFormat.Xml)],
17 // 并在操作正文中包括以下行:
18 // WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
19 [OperationContract]
20 public void DoWork()
21 {
22 // 在此处添加操作实现
23 return;
24 }
25 [OperationContract]
26 public string ReResult(string name)
27 {
28 return string.Format("123:{0}", name);
29 }
30 // 在此处添加更多操作并使用 [OperationContract] 标记它们
31 }

2.ASP.NET AJAX客户端

View Code
1 <script type="text/javascript">
2 function WCFAjaxServer() {
3 var name = $get("txtScript").value;
4 var ajaxClient = new AjaxWcf.AjaxWCFServer();
5
6
7 // $post("WCFAjaxService.svc/ReResult"
8 ajaxClient.ReResult(name, onSucceeded, onError);
9
10 }
11 function onSucceeded(reResult) {
12 $get("txtResult").value = reResult.toString();
13 }
14 function onError(reResult) {
15 alert("出错了!");
16 }
17 </script>
浏览器提示错误:
行: 12
错误: 'AjaxWcf' 未定义
调试错误提示:
Microsoft JScript 运行时错误‘AjaxWCF’未定义
------------
配置文件,命名空间,引用都全看了,而且也在demo里面创建,还是不行。应该是Ajax调用时出错,但不知道错在哪里。
弄了一个晚上,依然搞不定,为了多活两年,也是给前辈一个机会扶助国家栋梁。希望前辈百忙之中给我解决小问题....感激不尽。
IT小猪的主页 IT小猪 | 初学一级 | 园豆:77
提问于:2011-07-02 20:57
< >
分享
最佳答案
0

建议看看这篇文章,你的WCF服务写的有点问题,调用过程也有问题:

http://kb.cnblogs.com/page/50175/

收获园豆:15
artwl | 专家六级 |园豆:16736 | 2011-07-03 10:38
其他回答(2)
0

那肯定是AjaxWcf这个对象为找到了.

1.未引用包含AjaxWcf的js

2.AjaxWcf书写错误

收获园豆:10
慧☆星 | 园豆:5640 (大侠五级) | 2011-07-04 09:01
0

你的客户端代码 应该不正确。Ajax 都没有设置WCF所在的路径,如果没有设置WCF的路径,怎样访问WCF呢。

收获园豆:5
Henry_Wang | 园豆:157 (初学一级) | 2011-11-09 12:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册