jquery调用wcf并展示出数据

网上看了很多jquery调用wcf的例子,可能是主机的原因,我用的是gd主机,所以都没有成功,昨天自己搞了一天,终于成功了,现把方法和代码和大家分享

首选创能wcf,代码很简单,如下:

 
  
1 using System;
2   using System.Data;
3   using System.ServiceModel;
4   using System.ServiceModel.Activation;
5   using System.ServiceModel.Web;
6 using System.Web.Script.Services;
7
8 [ServiceContract(Namespace = "" )]
9 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
10 [ServiceBehavior(IncludeExceptionDetailInFaults = true )]
11 public class imgService
12 {
13 // 要使用 HTTP GET,请添加 [WebGet] 特性。(默认 ResponseFormat 为 WebMessageFormat.Json)
14 // 要创建返回 XML 的操作,
15 // 请添加 [WebGet(ResponseFormat=WebMessageFormat.Xml)],
16 // 并在操作正文中包括以下行:
17 // WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
18 [OperationContract]
19 [WebGet(RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
20 // [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.WrappedRequest)]
21 public DataTable GetDataTable( string pz)
22 {
23 // 在此处添加操作实现
24 DataSet ds = WebBase.GetDS(WebBase.meinvRepository, string .Empty, " body_id desc " ,pz);
25 if (ds.Tables.Count > 0 )
26 return ds.Tables[ 0 ];
27 return null ;
28 }
29
30 // 在此处添加更多操作并使用 [OperationContract] 标记它们
31 }

说明一下,wcf就一个GetDataTable方法,有一个参数pz表示返回数据的行数,返回的table以json数据给jquery使用

页面调用方法也很简单:

 
  
1 <% @ Page Language = " C# " EnableViewState = " false " AutoEventWireup = " true " CodeFile = " Default.aspx.cs "
2 Inherits = " jquerywcf_Default " %>
3 <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
4 < html xmlns = " http://www.w3.org/1999/xhtml " >
5 < head id = " Head1 " runat = " server " >
6 < title ></ title >
7 < script src = " ../Scripts/jquery-1.4.1-vsdoc.js " type = " text/javascript " ></ script >
8 </ head >
9 < body >
10 < form id = " form1 " runat = " server " >
11 < div class = " main " >
12 < ul >< li >< a ></ a ></ li ></ ul >
13 </ div >
14 < div >
15 < script type = " text/javascript " >
16 function sendAJAX(data) {
17 var url = " <%=_url %>imgService.svc/GetDataTable " ;
18
19 $.getJSON(url, { pz: " 6 " }, function (msg) {
20 $(msg.d).find( " Table " ).each(function (i) {
21 var rq = $( this ).children( " Body_AddTime " ).text().split( ' T ' )[ 0 ].replace( " - " , "" ).replace( " - " , "" );
22 rq = rq.substring( 0 , rq.length - 2 );
23 var html = ' <li><a href="http://meinv.vs2010.com/topic/ ' + $( this ).children( " Body_MuLuID " ).text() + ' / ' + rq + ' /show-id ' + $( this ).children( " Body_ID " ).text() + ' .html" target="_blank"><img src="http://meinv.vs2010.com/ ' + $( this ).children( " Body_Tu " ).text() + ' " /> ' + $( this ).children( " Body_Name " ).text() + ' </a></li> ' ;
24 $( " .main ul " ).append(html);
25 });
26 });
27
28 }
29
30
31 $(function () {
32 sendAJAX();
33 });
34 </ script >
35 < div id = " msg " >
36 </ div >
37 </ div >
38 </ form >
39 </ body >
40 </ html >

说明一下,主要是通过jquery的getjson方法去取得wcf返回的数据,传参方法:

$.getJSON(url, { pz: "6" }, function (msg) {alert(msg.d)});

这里是返回6条数据

还有wcf的svc上加上属性: Factory=System.ServiceModel.Activation.WebScriptServiceHostFactory

还有web.config的配置:

 
 
  
1 <? xml version="1.0" ?>
2 < configuration >
3
4 < configSections >
5 <!-- ajax -->
6 < sectionGroup name ="ajaxNet" >
7 < section name ="ajaxSettings" type ="AjaxPro.AjaxSettingsSectionHandler,AjaxPro.2" requirePermission ="false" restartOnExternalChanges ="true" />
8 </ sectionGroup >
9 <!-- ajax -->
10 < sectionGroup name ="system.web.extensions" type ="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" >
11 < sectionGroup name ="scripting" type ="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" >
12 < section name ="scriptResourceHandler" type ="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission ="false" allowDefinition ="MachineToApplication" />
13 < sectionGroup name ="webServices" type ="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" >
14 < section name ="jsonSerialization" type ="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission ="false" allowDefinition ="Everywhere" />
15 < section name ="profileService" type ="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission ="false" allowDefinition ="MachineToApplication" />
16 < section name ="authenticationService" type ="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission ="false" allowDefinition ="MachineToApplication" />
17 < section name ="roleService" type ="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission ="false" allowDefinition ="MachineToApplication" />
18 </ sectionGroup >
19 </ sectionGroup >
20 </ sectionGroup >
21 </ configSections >
22 < appSettings />
23 < connectionStrings >
24 < add name ="meinvvs2010ConnectionString" connectionString ="server=127.0.0.1;user id=test; password=test; database=test;pooling=false" providerName ="MySql.Data.MySqlClient" />
25 </ connectionStrings >
26 <!-- ajax -->
27 < ajaxNet >
28 < ajaxSettings >
29 < urlNamespaceMappings useAssemblyQualifiedName ="false" allowListOnly ="false" />
30 < jsonConverters includeTypeProperty ="true" />
31 < debug enabled ="false" />
32 < token enabled ="false" sitePassword ="password" />
33 </ ajaxSettings >
34 </ ajaxNet >
35 < location path ="ajaxpro" allowOverride ="true" inheritInChildApplications ="false" >
36 < system.web >
37 < globalization requestEncoding ="utf-8" responseEncoding ="utf-8" fileEncoding ="utf-8" />
38 <!-- for iis7
39 <system.webServer>
40 <handlers>
41 <add verb="*" path="*.ashx" name="AjaxPro" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
42 </handlers>
43 </system.webServer> -->
44 <!-- for iis6 -->
45 < httpHandlers >
46 < add verb ="*" path ="*.ashx" type ="AjaxPro.AjaxHandlerFactory,AjaxPro.2" />
47 </ httpHandlers >
48 </ system.web >
49 </ location >
50 <!-- ajax -->
51 < system.web >
52 < customErrors mode ="Off" />
53 < compilation debug ="true" >
54 < assemblies >
55 < add assembly ="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
56 < add assembly ="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
57 < add assembly ="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
58 < add assembly ="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
59
60 < add assembly ="System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
61 < add assembly ="System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
62 </ assemblies >
63
64 < buildProviders >
65 < remove extension =".svc" />
66 < add extension =".svc" type ="System.ServiceModel.Activation.ServiceBuildProvider,System.ServiceModel, Version=3.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089" />
67 </ buildProviders >
68 </ compilation >
69
70
71
72 <!--
73 通过 <authentication> 节可以配置
74 安全身份验证模式,ASP.NET
75 使用该模式来识别来访用户身份。
76 -->
77 < authentication mode ="Windows" />
78 <!--
79 如果在执行请求的过程中出现未处理的错误,
80 则通过 <customErrors> 节
81 可以配置相应的处理步骤。具体而言,
82 开发人员通过该节可配置要显示的 html 错误页,
83 以代替错误堆栈跟踪。
84
85 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
86 <error statusCode="403" redirect="NoAccess.htm" />
87 <error statusCode="404" redirect="FileNotFound.htm" />
88 </customErrors>
89 -->
90 < pages >
91 < controls >
92 < add tagPrefix ="asp" namespace ="System.Web.UI" assembly ="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
93 < add tagPrefix ="asp" namespace ="System.Web.UI.WebControls" assembly ="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
94 </ controls >
95 </ pages >
96
97 < httpHandlers >
98 < remove verb ="*" path ="*.asmx" />
99 < add verb ="*" path ="*.asmx" validate ="false" type ="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
100 < add verb ="*" path ="*_AppService.axd" validate ="false" type ="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
101 < add verb ="GET,HEAD" path ="ScriptResource.axd" type ="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate ="false" />
102 </ httpHandlers >
103 < httpModules >
104 < add name ="ScriptModule" type ="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
105 < add name ="ServiceModel" type ="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
106 </ httpModules >
107 </ system.web >
108 < system.codedom >
109 < compilers >
110 < compiler language ="c#;cs;csharp" extension =".cs" warningLevel ="4"
111 type ="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
112 < providerOption name ="CompilerVersion" value ="v3.5" />
113 < providerOption name ="WarnAsError" value ="false" />
114 </ compiler >
115 < compiler language ="vb;vbs;visualbasic;vbscript" extension =".vb" warningLevel ="4"
116 type ="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
117 < providerOption name ="CompilerVersion" value ="v3.5" />
118 < providerOption name ="OptionInfer" value ="true" />
119 < providerOption name ="WarnAsError" value ="false" />
120 </ compiler >
121 </ compilers >
122 </ system.codedom >
123 <!--
124 system.webServer 节是在 Internet Information Services 7.0 下运行 ASP.NET AJAX
125 所必需的。对早期版本的 IIS 来说则不需要此节。
126 -->
127 < system.webServer >
128 < validation validateIntegratedModeConfiguration ="false" />
129 < urlCompression doStaticCompression ="true" doDynamicCompression ="true" />
130
131 < httpErrors errorMode ="Detailed" />
132 < asp scriptErrorSentToBrowser ="true" />
133
134
135
136 < modules >
137 < remove name ="ScriptModule" />
138 < add name ="ScriptModule" preCondition ="managedHandler" type ="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
139 </ modules >
140 < handlers >
141 < remove name ="WebServiceHandlerFactory-Integrated" />
142 < remove name ="ScriptHandlerFactory" />
143 < remove name ="ScriptHandlerFactoryAppServices" />
144 < remove name ="ScriptResource" />
145 < remove name ="svc" />
146 < add name ="svc" path ="*.svc" verb ="*" type ="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition ="integratedMode" />
147
148 < add verb ="*" path ="*.ashx" name ="AjaxPro" type ="AjaxPro.AjaxHandlerFactory,AjaxPro.2" />
149 < add name ="ScriptHandlerFactory" verb ="*" path ="*.asmx" preCondition ="integratedMode"
150 type ="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
151 < add name ="ScriptHandlerFactoryAppServices" verb ="*" path ="*_AppService.axd" preCondition ="integratedMode"
152 type ="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
153 < add name ="ScriptResource" preCondition ="integratedMode" verb ="GET,HEAD" path ="ScriptResource.axd" type ="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
154 </ handlers >
155 </ system.webServer >
156 < runtime >
157 < assemblyBinding appliesTo ="v2.0.50727" xmlns ="urn:schemas-microsoft-com:asm.v1" >
158 < dependentAssembly >
159 < assemblyIdentity name ="MySql.Data" publicKeyToken ="C5687FC88969C44D" />
160 <!-- <bindingRedirect oldVersion="5.0.7.0" newVersion="6.3.2.0"/> -->
161 < bindingRedirect oldVersion ="5.0.7.0" newVersion ="6.2.3.0" />
162 </ dependentAssembly >
163 < dependentAssembly >
164 < assemblyIdentity name ="System.Web.Extensions" publicKeyToken ="31bf3856ad364e35" />
165 < bindingRedirect oldVersion ="1.0.0.0-1.1.0.0" newVersion ="3.5.0.0" />
166 </ dependentAssembly >
167 < dependentAssembly >
168 < assemblyIdentity name ="System.Web.Extensions.Design" publicKeyToken ="31bf3856ad364e35" />
169 < bindingRedirect oldVersion ="1.0.0.0-1.1.0.0" newVersion ="3.5.0.0" />
170 </ dependentAssembly >
171 </ assemblyBinding >
172 </ runtime >
173
174 < system.serviceModel >
175 < behaviors >
176 < endpointBehaviors >
177 < behavior name ="imgServiceAspNetAjaxBehavior" >
178 < enableWebScript />
179 </ behavior >
180 </ endpointBehaviors >
181 </ behaviors >
182 < serviceHostingEnvironment aspNetCompatibilityEnabled ="true" >
183 < baseAddressPrefixFilters >
184 <!-- <add prefix="http://demo.vs2010.com"/> -->
185 </ baseAddressPrefixFilters >
186 </ serviceHostingEnvironment >
187
188 < services >
189 < service name ="imgService" >
190 < endpoint address ="" behaviorConfiguration ="imgServiceAspNetAjaxBehavior"
191 binding ="webHttpBinding" contract ="imgService" />
192 </ service >
193 </ services >
194 </ system.serviceModel >
195 </ configuration >

好的,成功!上传到gd空间试试 :)

效果浏览 :http://demo.vs2010.com/jquerywcf/


转载于:https://www.cnblogs.com/vs2012/archive/2011/07/07/2099966.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值