/****************************************************************** * Copyright (C): 一心堂集团 * CLR版本: 4.0.30319.18063 * 命名空间名称: WcfService1 * 文件名: IJoonService * GUID1: b7bd3ab3-3668-4727-9416-f9845da207e1 创建人:尹明能 * 创建时间: 2014-9-24 13:13:09 ******************************************************************/ using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; namespace WcfService1 { //一定要取个名字,不然客户端访问不到 [ServiceContract(Namespace = "ymn", Name = "J")] public interface IJoonService { //暴漏方法,并且返回json格式数据 [OperationContract] JsonResult HelloWorld(); [OperationContract] string HelloWorld2(); [OperationContract] List<JsonResult> HelloWorld3(); } }
第一步,首先定义契约,创建一个接口
第二步实现接口
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; namespace WcfService1 { [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class JsonWCF:IJoonService { // 要使用 HTTP GET,请添加 [WebGet] 特性。(默认 ResponseFormat 为 WebMessageFormat.Json) // 要创建返回 XML 的操作, // 请添加 [WebGet(ResponseFormat=WebMessageFormat.Xml)], // 并在操作正文中包括以下行: // WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; // 在此处添加更多操作并使用 [OperationContract] 标记它们 [WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] public JsonResult HelloWorld() { JsonResult jr = new JsonResult(); jr.Address = "tt"; jr.Name = "rrr"; jr.Phone = "78"; // List<Admin> list = me.Admin.ToList(); return jr; } public string HelloWorld2() { return "啊啊"; } public List<JsonResult> HelloWorld3() { List<JsonResult> list = new List<JsonResult>(); JsonResult jr = new JsonResult(); jr.Address = "tt"; jr.Name = "rrr"; jr.Phone = "78"; JsonResult j = new JsonResult(); j.Address = "yy"; j.Name = "uu"; j.Phone = "000"; list.Add(jr); list.Add(j); return list; } } }
第三步另一个项目调用wcf服务,并解析json对象
1 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml" > 6 <head runat="server"> 7 <title>Index</title> 8 <script src="<%= Url.Content("~/Scripts/jquery-1.4.1.js") %>" 9 type="text/javascript"></script> 10 11 </head> 12 <body> 13 14 <form id="form1" runat="server"> 15 <div> 16 17 <br /> 18 <br /> 19 <input id="btnQueryDictionary" type="button" value="测试" onclick="btnQuery();" /> 20 <br /> 21 <br /> 22 消息: <p id="lblMsg"></p> 23 </div> 24 25 <script type="text/javascript"> 26 function btnQuery() { 27 28 //非ajax请求 29 // var wcfProxy = new ymn.J(); 30 // wcfProxy.HelloWorld(OnSucceededCallback, OnFailedCallback); 31 32 //ajax 请求 33 $("#lblMsg").html(""); 34 35 $.ajax({ 36 type: "post", 37 38 url: "http://localhost:2813/JsonWCF.svc/HelloWorld3", 39 contentType: "application/json;charset=utf-8", 40 data: "", //没有数据 41 success: function (data) { 42 $.each(data.d, function (i, item) { 43 $("#lblMsg").append(item.Address+"</br>"); 44 }); 45 }, 46 error: function (XMLHttpRequest, textStatus, errorThrown) { 47 $("#lblMsg").html("error"); 48 }, 49 cache: false 50 }); 51 52 } 53 function OnSucceededCallback(msg) { 54 //转换成json对象 55 var a = eval(msg); 56 $("#lblMsg").html(a.Address); 57 58 // $.each(a, function (i, item) { 59 // $("#lblMsg").append( 60 // item.Address+"<br/>" 61 // 62 // ); 63 // }); 64 // 65 } 66 67 function OnFailedCallback(error, userContext, methodName) { 68 alert("异常信息:" + error.get_message() + " " + 69 "异常类型:" + error.get_exceptionType() + " " + 70 "堆栈信息:" + error.get_stackTrace()); 71 } 72 </script> 73 74 75 <asp:ScriptManager ID="ScriptManager1" runat="server"> 76 <Services> 77 <asp:ServiceReference Path="http://localhost:2813/JsonWCF.svc" /> 78 </Services> 79 </asp:ScriptManager> 80 </form> 81 82 83 </body> 84 </html>
2009-10-22后记知道那个“d”是干嘛滴了,不过一直没有更新,为了以后复习,备注在此 [OperationContract( Name = "DoWork4" )] [WebInvoke( Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/DoWork4" )] [return: MessageParameter( Name = "e" )] public Person DoWork4( [MessageParameter( Name = "e" )] Person e ) { // 在此处添加操作实现 return new Person() { Age = 100, Name = "远始天尊" }; }加重的两行,[return: MessageParameter( Name = "e" )]这一行影响返回JSON对象时的名称,就是传说中的"d",此时被声明为"e"了后一行 public Person DoWork4( [MessageParameter( Name = "e" )] Person e ) {这个影响的是方法接收JSON对象的参数名称,也被改为"e"了,呵呵