jquery invokes a WCF service to pass a JSON object

Source: Internet
Author: User
Tags web services

Http://developer.51cto.com/art/200906/128643.htm

The following example uses WCF to create a service port that can be accessed by the ASP.net page through a jquery Ajax approach, and we will use AJAX technology on the client to communicate with the WCF service. Here we only use jquery to connect to the Web service instead of using the ASP.net Ajax Library, as for why we don't use AJAX libraries, because we've already used jquery in our projects, and it's already able to handle all the Ajax requests and all the functionality, and If we were to use the ASP.net Ajax library, we would also have to include more than 80Kb of data (more in debug mode), but that's not to say that ASP.net Ajax libraries are not practical, in fact, if you use the same class library, we can write a lot of extra code less, But this example is to illustrate how we invoke Web services without a good client agent.

WCF Services:

We first create a website and then add a ajax-enabled WCF service to create a WCF services. (Make sure you're using the correct. Net Framework version, I'm using 3.5)

After we have added the service, it automatically enters the post code file for the service, and goes ahead and browse around the file for a second.

The first thing we have to do is to find "aspnetcompatibilityrequirements" and set its value to "allowed":

[Aspnetcompatibilityrequirements (Requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]

This property is set to run our service in the ASP.net compatibility mode, and if we do not set the value to "allowed", then we cannot access the service through ASP.net, which is when you add "ajax-enabled WCF Service "is generated automatically. Please refer to MSDN for more details.

Looking at the automatically generated post code file, we can see that there is already a "DoWork ()" method labeled "OperationContract", which is automatically generated and we will use this method to complete the following example. Let's add a "WebGet" attribute to the method and set the value of "Requestformat" to "Json." We'll add another feature to the method requestformat,webget and get verbs, Acting on a uritemplate (this article does not discuss this further), the Requestformat feature allows us to receive JSON-formatted requests.

Our "DoWork ()" method is as follows:

[OperationContract] [WebGet (Requestformat=webmessageformat.json)] public void DoWork () {//ADD your operation implementation h    ere return; }

object's Model:

We want to pass "DoWork ()" to an object called "person", first to create a person object written to the head of the current class, which contains fields and attributes (Name, age, and the types of Shoes they own), This class also acts as the structure of the JSON that is passed.

[serializable]    [DataContract ( Namespace =  "http://www.dennydotnet.com/",  name  =  "Person"  )]    public class person    {         private string _name = string. empty;        private int _age = 0;           [datamember ( IsRequired = true, Name =  "Name"  )]        public string Name        {            get { return _name; }             set { _name = value; }         }          [datamember ( isrequired = true,  Name =  "Age"  )]        public int age        {             get { return _age; }             set { _age = value; }         }          [datamember ( isrequired = true, name  =  "Shoes"  )]        public List<String> Shoes;    }         

We have signed the contract for the name and namespace of the "person" class, and we still need to give the attribute to the data member attribute, set "IsRequired" for each property, and specify its name. You only need to specify the name, if it is not the same as the property name. For example, if you have an attribute called level, but you assign the value to "Rank" in the data member attribute, now we go back and modify our "DoWork ()" method to receive the person object as an argument. The code block below the specific parameter.

[OperationContract] [WebGet (Requestformat=webmessageformat.json)] public void DoWork (person p) {//ADD your operation Implemen    Tation here return; }

To configure the Web.config file:
We only need to make very small changes to the Web.config file to be able to access the service. First join a servicebehavior to allow HTTP GET requests, and then add some debugging options to help. The code is as follows:

Below </endpointBehaviors>

<serviceBehaviors> <behaviorName= "Serviceaspnetajaxbehavior" > <servicemetadataHttpgetenabled= "true" Httpgeturl= "" /> <servicedebugHttphelppageenabled= "true" includeexceptiondetailinfaults= "true" />

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.