News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. ***
  May 30 2015 1:04PM     John
  0 Comments    1073 Views  
In this article John demonstrates, how to call a simple WCF Service from jQuery in C#. The following is the procedure.
Create Service Contract
1. Launch any edition of Visual Studio 2010 or 2012.

2. Create a project by choosing WCF Service Application project template from WCF tab.

3. Let us give the WCF Service Name "MySampleWCFService".

4. Ensure the attribute of the Interface is set as "[ServiceContract]".

5. Define the functions you want to create as part of the Contract Set the Attribute of the functions as "[OperationContract]"
You will create an IService1 Service Contract with the four basic calculator operations as the following.
IService1.cs
namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        string GetData(int value);

        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        string[] GetUser(string Id);
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}
You have created a Service Contract. These contracts will be part of the Service Contract and exposed to clients. By this step you have created the IService Service Contract.
Implement the Service
IService1.svc.cs
Replace the getdata function with the following code.
namespace WcfService1
{

    [AspNetCompatibilityRequirements(RequirementsMode
    = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        public string GetData(int Id)
        {
            return string.Format("You entered: {0}", Id);
        }

        
        public string[] GetUser(string Id)
        {
            return new User().GetDetails(Convert.ToInt32(Id));
        }
    }

    public class User
    {

        Dictionary<intstring> users = null;
        public User()
        {
            users = new Dictionary<intstring>();
            users.Add(1, "The");
            users.Add(2, "Developer");
            users.Add(3, "Tips");
            users.Add(4, "Beyond The knowledge");
        }

        public string[] GetDetails(int Id)
        {
            var user = from u in users
                       where u.Key == Id
                       select u.Value;

            return user.ToArray<string>();
        }

    }
}
Now you need add a new page Default.aspx and add the below code in the Javascript.
Default.aspx
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

    <script type="text/javascript">

        $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "Service1.svc/GetUser",
            data: {"Id": 1},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processdata: true,
            success: function (msg) {

                if (msg == "json") {
                    var resultObject = msg.GetUserResult;

                    for (i = 0; i < resultObject.length; i++) {
                        alert(resultObject[i]);
                    }

                }
            },
            error: function (result) {
                alert('Service call failed: ' + result.status + '' + result.statusText);
            }
        });
    });
    </script>
To call WCF Service, press F5 in Visual Studio and WCF Service will be calling in Java Script page load.
I hope this page will helps you to call WCF service from jQuery in C#.Thanks.
BackToTop
Comments



 
Search
1&1 Hosting
Recent Posts
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2016 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2016 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2016 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2016 10:20AM Posted By Micheal
Read excel file and convert to List in C#
Feb 25 2016 6:26AM Posted By Michael
Encryption and Decryption for Password in C#
Feb 17 2016 3:52AM Posted By Michael
How to create Chart in ASP.Net C#
Feb 16 2016 8:03AM Posted By Michael Ryan
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: