调用WCF返回Josn的两种方式
?
開始之前先定義個泛型類和數(shù)據(jù)對象類型
Code????[DataContract]
????public?class?Product
????{
????????[DataMember]
????????public?int?ProductID;
????????[DataMember]
????????public?string?Name;
????????[DataMember]
????????public?string?ProductNumber;
????}
????[DataContract]
????public?class?PageData<T>
????{
????????[DataMember]
????????public?int?TotolRecord
????????{
????????????get;
????????????set;
????????}
????????[DataMember]
????????public?T?Data
????????{
????????????get;
????????????set;
????????}
????}
?
方法一:
參考:
http://www.cnblogs.com/iamv/archive/2008/10/31/1323821.html
http://www.cnblogs.com/jillzhang/archive/2008/06/13/1219201.html
新建Ajax-enabled WCF Service
名字為ProductService.svc
WCF代碼
Code
using?System;???
using?System.IO;???
using?System.Runtime.Serialization;???
using?System.ServiceModel;???
using?System.ServiceModel.Activation;???
using?System.Data.SqlClient;???
using?System.Runtime.Serialization.Json;
using?System.ServiceModel.Web;
namespace?Josn_WCF
 {
 ????[ServiceContract(Namespace?=?"")]
 ????[AspNetCompatibilityRequirements(RequirementsMode?=?AspNetCompatibilityRequirementsMode.Allowed)]
 ????public?class?ProductService
 ????{
 ????????[OperationContract]
 ????????public?string?GetProductDetailsByProductID(int?productID)
 ????????{
//隨便定義一個返回的對象
 ????????????Product?prod?=?new?Product();
 ????????????int?i?=?productID;
 ????????????prod.ProductID?=?i;
 ????????????prod.Name?=?i.ToString()?+?"_V";
 ????????????prod.ProductNumber?=?i.ToString()?+?"007";
 ????????????MemoryStream?stream?=?new?MemoryStream();
 ????????????DataContractJsonSerializer?serializer?=?new?DataContractJsonSerializer(typeof(Product));
 ????????????serializer.WriteObject(stream,?prod);
 ????????????stream.Position?=?0;
 ????????????StreamReader?streamReader?=?new?StreamReader(stream);
 ????????????return?streamReader.ReadToEnd();
 ????????}
 ????}
 }
?
UI調(diào)用WCF代碼
新建一個aspx頁面
?
Code<%@?Page?Language="C#"?AutoEventWireup="true"?CodeBehind="JSON_WCF.aspx.cs"?Inherits="Josn_WCF.JSON_WCF"?%>
<!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?id="Head1"?runat="server">
????<title>Invoking?the?Product?Service?through?the?AJAX?based?Client</title>
????<script?type="text/javascript">???????
???function?pageLoad()?{???
???}???
???function?OnGetClick()?{????????????????????
?????ProductService.GetProductDetailsByProductID($get("txtProductID").value,OnGetComplete,?OnError);????????????
???}????
???function?OnGetComplete(result){????
?????var?prod?=?eval("("?+?result?+?")");???????????????????
?????$get("spnProductID").innerText?=?prod.ProductID;???
?????$get("spnProductName").innerText?=?prod.Name;???
?????$get("spnProductNumber").innerText?=?prod.ProductNumber;???????????????????
???}????
???function?OnError(errorMessage)?{???
?????alert(errorMessage.get_message());????
???}????
????</script>
</head>
<body>
????<form?id="form1"?runat="server">
????<div>
????????Enter?Product?ID:
????????<input?type="text"?id="txtProductID"?name="txtProductID"?/>
????????<input?type="button"?value="Get?Product?Details"?id="btnInvokeWebService"?onclick="OnGetClick()"?/>
????????<asp:ScriptManager?ID="ScriptManager1"?runat="server">
????????????<Services>
????????????????<asp:ServiceReference?Path="~/ProductService.svc"?/>
????????????</Services>
????????</asp:ScriptManager>
????????<br?/>
????????<br?/>
????????Product?ID?:?<span?id="spnProductID"></span>
????????<br?/>
????????<br?/>
????????Name?:<span?id="spnProductName"></span>
????????<br?/>
????????<br?/>
????????Product?Number?:<span?id="spnProductNumber"></span>
????????<br?/>
????????<br?/>
????</div>
????</form>
</body>
</html>
?
主要是注意其中的JavaScript代碼和ScriptManager
<asp:ScriptManager?ID="ScriptManager1"?runat="server">
?????<Services>
??????????<asp:ServiceReference?Path="~/ProductService.svc"?/>
 ?????</Services>
</asp:ScriptManager>
?
轉(zhuǎn)載于:https://www.cnblogs.com/iamv/archive/2008/11/06/1327941.html
總結(jié)
以上是生活随笔為你收集整理的调用WCF返回Josn的两种方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 五年磨一剑未成
- 下一篇: oralce或sql中join的用法
