ExtJS Learning Notes (20)-Leverage ExtJS Ajax to interact with service-side WCF

Source: Internet
Author: User
Tags json sleep thread

ExtJS is a very good JavaScript UI library (the first contact with ExtJS, you can go to the official website http://www.extjs.com/deploy/dev/examples/samples.html look at the example. I believe many people will be enchanted, not only the component is rich, the effect is beautiful, and ExtJS integrated Ajax function can easily interact with. NET WCF.

Here we will demonstrate ExtJS's Formpanel loading data from WCF and how to submit data to the WCF server

1. First, define a class for transferring information (in actual development, you can be a class of LINQ to SQL or any Serializable entity class)

[DataContract]
  public class MyData
  {
    [DataMember]
    public string id;
    [DataMember]
    public string text;
  }

Very simply, only two member Id,text are defined in MyData, plus [DataContract] and [DataMember] indicate that the class can be serialized

2. Redefine several methods used to interact with ExtJS (first contact with the comrades of Ajax and WCF interaction, suggest a reference to Lao Zhang's "Ajax and WCF interactive-WCF beauty" (http://www.cnblogs.com/jillzhang/archive/ 2008/06/13/1219201.html) ")

[ServiceContract (Namespace = "")]
[Aspnetcompatibilityrequirements (Requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]
public class MyService
{
[OperationContract]
[WebInvoke (Responseformat = Webmessageformat.json, method = "*", UriTemplate = "Getmydata")]
Public MyData Getmydata ()
{
System.Threading.Thread.Sleep (1000);
MyData _node = new MyData () {id = "1", Text = "Test Text"};
return _node;
}

/**////<summary>
RESTFul WCF is used to get-way to obtain ExtJS submitted data (Json)
</summary>
<param name= "id" ></param>
<param name= "Text" ></param>
<returns></returns>
[OperationContract]
[WebInvoke (method = "*", Responseformat = Webmessageformat.json, UriTemplate = "Savemydata?id={id}&text={text}")]
Public MyData Savemydata (string id,string text)
{
System.Threading.Thread.Sleep (1000);
MyData _node = new MyData () {id = id, text = text};
return _node;
}

/**////<summary>
Post method to process data submitted by EXTJS (JSON format)
</summary>
<param name= "Input" ></param>
<returns></returns>
[OperationContract]
[WebInvoke (method = "*", Responseformat = Webmessageformat.json, UriTemplate = "SaveMyData2")]
Public MyData SaveMyData2 (Stream input)
{
string s = "";
using (StreamReader sr = new StreamReader (input))
{
s = Sr. ReadToEnd ();
}
NameValueCollection qs = httputility.parsequerystring (s);
String id = qs["id"];
string text = qs["text"];

System.Threading.Thread.Sleep (1000);
MyData _node = new MyData () {id = id, text = text};
return _node;
}
}

Related Article

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.