Click here to Skip to main content
16,018,534 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
I have two solutions one is RestWCFService and another is ConsumeRestService

the code of RestWCFService

Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RestWCFService
{
    public class Employee
    {

        public int EmpId { get; set; }

        public string EmployeeName { get; set; }

        public string Designation { get; set; }

        public string Projects { get; set; }



    }
}

IRestServiceExample.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;

namespace RestWCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRestServiceExample" in both code and config file together.
    [ServiceContract]
    public interface IRestServiceExample
    {
        [OperationContract]
        [WebInvoke
        (Method = "GET",
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "xml/{id}")
         ]
        string xmlData(string id);


        [OperationContract]
        [WebGet
        (
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "json/{id}")
         ]
       string jsonData(string id);


        [OperationContract]
        [WebInvoke(Method = "GET",
                  ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Name/{Name}")]
        List<Employee> GetEmployeeInfromation(string Name);



    }
}


RestServiceExample.SVC.CS




using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace RestWCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "RestServiceExample" in code, svc and config file together.
    public class RestServiceExample : IRestServiceExample
    {

        public string xmlData(string x)
        {

            return "Hi " + x  + " how r u ??";
        }


        public string jsonData(string x)
        {

            return "Hi " + x + " how r u ??";
        }


        public List<Employee> GetEmployeeInfromation(string Name)
        {

            List<Employee> emp = new List<Employee>
                                 { new Employee {EmpId=1, EmployeeName="XCD", Designation="Consultant",Projects="KKK" },
                                  new Employee {EmpId=2, EmployeeName="ABC", Designation="Sr. Consultant",Projects="KKK" },
                                   new Employee {EmpId=3, EmployeeName="XYZ", Designation="Sr. Consultant",Projects="JHT" }};
            var k = emp.Where(e => e.EmployeeName == Name);
            return k.ToList();
        }








    }
}


code ConsumeService solution

Test4.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test4.aspx.cs" Inherits="ConsumeRestService.Test4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <script  type="text/javascript" src="Scripts/jquery-1.10.2.min.js"></script>
    <title></title>
 <%--   <script runat="server" type="text/javascript" src="~/jquery-1.7.2.min.js"></script>--%>
    <script type="text/jscript">

        $(document).ready(function () {
            $("#btnReterive").click(function () {
                var enterInformation = $("#empText").val();
                var url = 'http://localhost/ConsumRestService/RestServices/RestServiceExample.svc/Name/' + enterInformation;
                alert(url);
                $.ajax(
                {
                    type: "GET",
                    url: url,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: successRecords,
                    error: errorRecords
                });

                function successRecords(data) {
                    $("#employeeInformation").append(data);
                    $("#employeeInformation").text(data);
                    $("#lblTest").val(data);
                    $("#lblTest").text(data);
                }


                function errorRecords(message) {
                    alert(message);
                }
            });


        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <label for="empText">
            EmployeeName :
        </label>
        &nbsp;&nbsp;
        <input type="text" id="empText" />
        &nbsp;&nbsp;
        <input type="button" id="btnReterive" />
        <label id="lblTest"></label>
        <div id="employeeInformation" style="font-size: 12px; font-family: Verdana; color: Aqua">

        </div>
    </div>
    </form>
</body>
</html>


my purpose is to append the data in label or div but proper format data is not coming.

kindly guide me where I am wrong in  my code
Posted
Updated 11-Sep-13 19:57pm
v2
Comments
Sergey Alexandrovich Kryukov 12-Sep-13 1:41am    
Where is your question?
—SA
aryan_isml29 12-Sep-13 1:46am    
my friend question is where am I wrong in above code???
why I can't received the proper data. I received only [object object] why so. Please corrected it need guidance for you.

1 solution

In client side success fuction instead of append, loop through the response and append to the html..

JavaScript
function successRecords(data) {
                 var finalData = '';
                 for(var i=0; i<data.length;i++){>
                   finalData =finaldata + data[i].EmployeeName+' ' +data[i].Designation ....
                 }
                    $(&quot;#employeeInformation&quot;).append(finalData );
                    $(&quot;#employeeInformation&quot;).text(finalData );
                    $(&quot;#lblTest&quot;).val(finalData );
                    $(&quot;#lblTest&quot;).text(finalData );
                }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900