基于ScriptManager的Ajax调用本地WCF

ZAIJIANLUOYE110 2014-05-08 07:14:04
按照网上教程写了一个小程序,但是运行出错。


下面是定义在Service1(启用了Ajax的WCF服务)中的代码:
namespace WebApplication1
{
[ServiceContract(Namespace = "myNamespace")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
// 要使用 HTTP GET,请添加 [WebGet] 特性。(默认 ResponseFormat 为 WebMessageFormat.Json)
// 要创建返回 XML 的操作,
// 请添加 [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// 并在操作正文中包括以下行:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public string SayHello(string name)
{
// 在此处添加操作实现
return string.Format("Hello : {0}",name);
}

// 在此处添加更多操作并使用 [OperationContract] 标记它们
}
}


下面是定义在WebForm1.aspx中的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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 runat="server">
<title></title>
<script type="text/javascript">
function Button_Click() {
var client = new myNamespace.Service1();
var name = "name1234";
client.SayHello(name, succeededCallback, failedCallback);
}
function succeededCallback(result) {
alert(result);
}
function failedCallback(result) {
alert(result);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Service1.svc" />
</Services>
</asp:ScriptManager>
<div>
<input type="submit" id="myButton" value="click" onclick="Button_Click()" />
</div>
</form>
</body>
</html>



在Firefox中调试js代码,client.SayHello是存在的,但是调用的时候alert对话框(显示的内容好像是两个Object)闪一下就没了,页面好像刷新了。
...全文
159 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
bwangel 2014-05-08
  • 打赏
  • 举报
回复
不要用这个劳什子的ScriptManager了,微软自己都抛弃了。 现在是jquery+mvc的天下。

17,748

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧