ExtJS Learning Notes (-SCRIPTTAGPROXY+XTEMPLATE+WCF) cross-domain fetching data

Source: Internet
Author: User
Tags datetime domain

Ajax applications in the Cross-domain has been a very troublesome problem, there are some solutions, but either more cumbersome, or not universal, fortunately ExtJS in the Scripttagproxy provides cross-domain reading data, and in several large browsers can be normal operation, But in the use of the process to pay attention to several points:

1. When the server returns, it must be returned in the following format:

StcCallback1001 ({...})

One of the stcCallback1001 1001 is automatically generated, if the page is submitted, each request again 1001 will become 1002,1003 ... analogy

Although there are many examples of scripttagproxy in the official 2.ExtJs example, there is a single feature sample that does not have xtemplate+scripttagproxy read across domains, and the following example of a xtemplate reading data across domains

A. Service-side WCF processing

Code
[OperationContract]
[WebInvoke (Responseformat = Webmessageformat.json, UriTemplate = "getdata3?start={start}&limit={limit}& Callback={callback} ", method =" * ")]
Public Stream GetData3 (int start, int limit,string CallBack)
{
System.Threading.Thread.Sleep (1000);//For demo Ajax load effect, stop 1 seconds
list<t_guestbook> _list = new list<t_guestbook> ();
_list.add (New T_guestbook () {f_id = 1, f_ip = "192.23.37.41", f_date = DateTime.Now, f_content = "This is the first message", f_reply = "" });
_list.add (New T_guestbook () {f_id = 2, f_ip = "192.168.0.1", f_date = DateTime.Now, f_content = "This is the second message", F_reply = ""} );
_list.add (New T_guestbook () {f_id = 3, f_ip = "192.168.0.2", f_date = DateTime.Now, f_content = "This is the third message", F_reply = ""} );
_list.add (New T_guestbook () {f_id = 4, f_ip = "172.168.235.1", f_date = DateTime.Now, f_content = "This is fourth message", f_reply = "" });
_list.add (New T_guestbook () {f_id = 5, f_ip = "10.200.30.4", f_date = DateTime.Now, f_content = "This is fifth message", f_reply = ""} );
_list.add (New T_guestbook () {f_id = 6, f_ip = "10.200.30.5", f_date = DateTime.Now, f_content = "This is sixth message", f_reply = ""} );
You can also use LINQ to read directly from the database
pagedata<list<t_guestbook>> _pagedata = new pagedata<list<t_guestbook>> ();
_pagedata.recordcount = _list.count;
int PageSize = limit;
if (PageSize <= 0) {PageSize = 1;}
if (PageSize > _pagedata.recordcount) {PageSize = _pagedata.recordcount;}
_pagedata.pagesize = PageSize;
Calculate Total Pages
if (_pagedata.recordcount% _pagedata.pagesize = 0)
{
_pagedata.pagecount = (_pagedata.recordcount/_pagedata.pagesize);
}
Else
{
_pagedata.pagecount = (_pagedata.recordcount/_pagedata.pagesize) + 1;
}
int PageIndex = (start/limit) + 1;
if (PageIndex <= 0) {PageIndex = 1;}
if (PageIndex > _pagedata.pagecount) {PageIndex = _pagedata.pagecount;}
_pagedata.currentpageindex = PageIndex;
list<t_guestbook> _list2 = _list.skip (start). Take (limit). ToList ()//Get current page data
_pagedata.data = _list2;
String returnstr = CallBack + "(" + javascriptconvert.serializeobject (_pagedata) + ")";
MemoryStream 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;
}

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.