In the previous article ExtJS Cross-domain access to and from WCF has solved the problem of ExtJS and WCF Cross-domain access through a service-side proxy, and that scenario does not look elegant, and when I wrote a restful way to invoke WCF for upload downloads, Stunned to find that the original WCF supports the return of native data (raw), which solves the challenge of Cross-domain calls between ExtJS and WCF: The return data must meet the <script> format. The following is based on the project implemented in Cross-domain access between ExtJS and WCF, using the combination of stream and contenttype to return native data to ExtJS for Cross-domain invocation.
The first step: in the Pagegridservice.svc background code, add the Operation contract Getproductsbypagecorssdomain, the code is:
Getproductsbypagecorssdomain method
[OperationContract]
[WebInvoke (method = "*", Responseformat = Webmessageformat.json,
UriTemplate = "Getproductsbypagecorssdomain?start={start}&limit={limit} &callback={callback}")]
Public Stream getproductsbypagecorssdomain (int start, int limit,string callback)
{
Productsdatacontext productdbcontext = new Productsdatacontext ();
iqueryable<product> res = productDbContext.Product.Select (product => product);
pagedata<product[]> returndata = new pagedata<product[]> ();
Returndata.totolrecord = Res. Toarray<product> (). Length;
res = Res. Skip<product> (start);
res = Res. Take<product> (limit);
Returndata.data = Res. Toarray<product> ();
System.Runtime.Serialization.Json.DataContractJsonSerializer Formater = new System.Runtime.Serialization.Json.DataContractJsonSerializer (typeof (Pagedata<product[]>));
MemoryStream ms = new MemoryStream ();
Formater. WriteObject (MS, Returndata);
Ms. Position = 0;
StreamReader sr = new StreamReader (MS);
String objcontent = Sr. ReadToEnd ();
String returnstr = callback+ "(" +objcontent+ ")";
Sr. Close ();
ms = new MemoryStream ();
StreamWriter sw = new StreamWriter (MS);
Sw. AutoFlush = true;
Sw. Write (RETURNSTR);
Ms. Position = 0;
WebOperationContext.Current.OutgoingResponse.ContentType = "Text/plain";
return MS;
}
Step Two: Create a new HTM page in the project: Pagegridcorssdomainwithrow.htm, the code is:
Pagegridcorssdomainwithrow.htm
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>extjs+wcf+linq to create pagination grid</title>
<link rel= "stylesheet" type= "Text/css" href= "Resources/css/ext-all.css"/>
<script type= "Text/javascript" src= "Adapter/ext/ext-base.js" charset= "gb2312" ></script>
<script type= "Text/javascript" src= "Ext-all-debug.js" charset= "gb2312" ></script>
<link rel= "stylesheet" type= "Text/css" href= "Shared/examples.css"/>
<script type= "Text/javascript" src= "Shared/examples.js" charset= "gb2312" ></script>
<script type= "Text/javascript" src= "Pagegridcrossdomainwithrow.js" charset= "gb2312" ></script>
<body>
Extjs+wcf+linq to create a paging cross-domain grid<div id= "Page-grid" >
</div>
</body>