自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF與ExtJs實現(xiàn)項目分析

開發(fā) 后端
這里就WCF與ExtJs之間的跨域訪問中實現(xiàn)的項目,通過Stream和ContentType的聯(lián)合使用,返回原生數(shù)據(jù)給Extjs,從而實現(xiàn)跨域調用。

大家可能對理論的知識都很熟,但是往往實踐起來就很困難,我們平時就要把實踐和理論相結合,在前面文章WCF與ExtJs之間的跨域訪問已經通過服務端代理的方式解決了WCF與ExtJs跨域訪問的問題。

#T#那個方案看起來并不怎么優(yōu)雅,而當我在寫過用Restful方式調用WCF進行上傳下載后,愕然發(fā)現(xiàn)原來WCF支持原生數(shù)據(jù)(Raw)的返回,這就解決了ExtJs與Wcf之間進行跨域調用中的難題:返回數(shù)據(jù)必須滿足<script>格式。下面根據(jù)WCF與ExtJs之間的跨域訪問中實現(xiàn)的項目,通過Stream和ContentType的聯(lián)合使用,返回原生數(shù)據(jù)給Extjs,從而實現(xiàn)跨域調用。

***步:在PageGridService.svc后臺代碼中,添加操作契約GetProductsByPageCorssDomain,代碼為:

  1. [OperationContract]  
  2. [WebInvoke(Method = "*"ResponseFormat = WebMessageFormat.Json,  
  3. UriTemplate = "GetProductsByPageCorssDomain?start={start}&limit={limit}&callback={callback}")]  
  4. public Stream GetProductsByPageCorssDomain(int start, int limit,string callback)  
  5. {  
  6. ProductsDataContext productDbContext = new ProductsDataContext();  
  7. IQueryable<Product> res = productDbContext.Product.Select(product => product);  
  8. PageData<Product[]> returnData = new PageData<Product[]>();  
  9. returnData.TotolRecord = res.ToArray<Product>().Length;  
  10. resres = res.Skip<Product>(start);  
  11. resres = res.Take<Product>(limit);  
  12. returnData.Data = res.ToArray<Product>();  
  13. System.Runtime.Serialization.Json.DataContractJsonSerializer formater = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(PageData<Product[]>));  
  14. MemoryStream ms = new MemoryStream();  
  15. formater.WriteObject(ms, returnData);  
  16. ms.Position = 0;  
  17. StreamReader sr = new StreamReader(ms);  
  18. string objContent = sr.ReadToEnd();  
  19. string returnStr = callback+"("+objContent+")";  
  20. sr.Close();  
  21. ms = new MemoryStream();  
  22. StreamWriter sw = new StreamWriter(ms);  
  23. sw.AutoFlush = true;  
  24. sw.Write(returnStr);  
  25. ms.Position = 0;  
  26. WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";  
  27. return ms;  

第二步:在項目中創(chuàng)建一個新的htm頁面:PageGridCorssDomainWithRow.htm,代碼為:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml" > 
  3. <head> 
  4. <title>ExtJs+WCF+LINQ打造分頁Grid</title> 
  5. <link rel="stylesheet" type="text/css" href="resources/css/ext-all.css" /> 
  6. <script type="text/javascript" src="adapter/ext/ext-base.js" charset="gb2312"></script> 
  7. <script type="text/javascript" src="ext-all-debug.js" charset="gb2312"></script> 
  8. <link rel="stylesheet" type="text/css" href="shared/examples.css" /> 
  9. <script type="text/javascript" src="shared/examples.js" charset="gb2312"></script> 
  10. <script type="text/javascript" src="PageGridCrossDomainWithRow.js" charset="gb2312"></script> 
  11. </head> 
  12. <body> 
  13. <h1> 
  14. ExtJs+WCF+LINQ打造分頁跨域Grid</h1> 
  15. <div id="page-grid"> 
  16. </div> 
  17. </body> 
  18. </html> 
責任編輯:田樹 來源: 博客
相關推薦

2021-01-20 05:46:51

自動化Coco項目

2023-11-30 11:45:07

大數(shù)據(jù)ODPS

2009-11-05 17:00:34

WCF有序傳遞

2010-05-11 09:29:44

數(shù)據(jù)中心節(jié)能項目

2010-03-02 13:36:23

WCF事務投票

2010-02-25 13:48:23

WCF動態(tài)創(chuàng)建代碼

2009-11-05 14:40:08

WCF KnownTy

2009-11-06 09:22:46

WCF應用

2009-11-06 13:33:56

WCF Dispatc

2009-11-05 10:37:58

WCF Address

2010-02-26 16:20:56

WCF程序事務

2009-12-08 14:28:16

XTemplate +

2009-11-05 13:21:41

WCF架構

2009-11-05 13:54:07

WCF Service

2009-12-08 18:31:58

WCF WEB

2012-04-20 10:05:16

WCF

2009-12-21 10:00:46

WCF基礎開發(fā)

2010-02-25 13:35:27

WCF tcpTrac

2010-02-24 12:49:39

WCF枚舉

2009-11-05 16:01:51

WCF單調服務
點贊
收藏

51CTO技術棧公眾號