And again, cross-domain access between ExtJS and WCF

Source: Internet
Author: User
Tags json serialization

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.