Sunday 2 February 2014

Call Wcf Rest services using jquery Ajax

    <section class="featured">

        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Title.</h1>
                <h2>@ViewBag.Message</h2>
            </hgroup>
            <p>
                To learn more about ASP.NET MVC visit
                <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
                The page features <mark>videos, tutorials, and samples</mark> to help you get the most                     from ASP.NET MVC.
                If you have any questions about ASP.NET MVC visit
                <a href="http://forums.asp.net/1146.aspx/1?MVC" title="ASP.NET MVC Forum">our forums</a>.
            </p>
        </div>
    </section>
    <input type="text" id="LoginId" />
    <input type="text" id="Name" />
    <input type="submit" id="submit" />
  

}
<h2>Here is my Ajax Call:</h2>
<script type="text/javascript">
    $(document).ready(function () {
        $("#submit").click(function () {
            var input = $("#LogInID").val();
            $.ajax({
                cache: false,
                type: "GET",
                async: false,
                url: "http://localhost:7045/Service1.svc/GetData/"+input+"",
               //data:JSON.stringify(input) ,
                dataType: "json",
                success: function (data) {
                    viewModel.DiplayEmp(data.DiplayEmp);
                    //var user = JSON.stringify(data);
                    //alert(user);
                },
                error: function (xhr) {
                    alert(xhr);
                }
            });
        });
      
    });
</script>

hiii guys here i am for posting my work i am working as software developer at lobaan software

<h2>In this company my first task is Create WCF Rest Service And call this service method  using jquery Ajax</h2>
So I m Posting this demo here its successfully work
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        public Employee AddEmployee(string  id)
        {
            var data = db.Emps.Where(e => e.EID ==Convert.ToInt32( id)).Select(e => e);

            return new Employee { EmpAddress = data.SingleOrDefault().EApproval, EmpName = data.SingleOrDefault().EName };
        }
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<h1>Here Interface of my service</h1>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService2
{
    // 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]
        [WebGet(ResponseFormat=WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json,UriTemplate="GetData/{id}",BodyStyle=WebMessageBodyStyle.Wrapped)]
        Employee AddEmployee(string  id);


        // TODO: Add your service operations here
    }
    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class Employee
    {
        [DataMember]
        public string EmpName { get; set; }
        [DataMember]
        public string EmpAddress { get; set; }
    }
}