-1

I am trying to store all information of my "user" table in wcf. and return it in json format.When i am invoking the service,i can see the entities. But my question is how to call this service in my view.I have tried it calling with ajax but all that shoots is the error message :( plzz help

service.svc.cs

namespace jsonwcf
{

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        [WebInvoke(
       Method = "POST",
       ResponseFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.WrappedRequest
     )]
        public List<UserDetails> SelectUserDetails()
        {
            pasDataContext db = new pasDataContext();
            List<UserDetails> results = new List<UserDetails>();

            foreach (User u in db.Users)
            {
                results.Add(new UserDetails()
                {
                    UserID = u.UserID,

                    EmpName = u.EmpName,
                    Email = u.EmailID,
                    UserName = u.UserName,
                    UserRole = u.UserRole,
                    Password = u.Password,
                    Telephone = u .Telephone
                });
            }
            return results;
        }
    }
}

iservice.cs

namespace jsonwcf
{
    // 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 = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "users")]

        List<UserDetails> SelectUserDetails();
    }
    [DataContract]
    public class UserDetails
    {     
        [DataMember]    
        public string UserID
        {    
            get;    
            set;    
        }

        [DataMember]
        public string Password
        {    
            get;    
            set;    
        }

        [DataMember]    
        public string UserName
        {    
            get;    
            set;    
        }

        [DataMember]    
        public string Email
        {    
            get;    
            set;    
        }

        [DataMember]
        public string EmpName
        {    
            get;    
            set;    
        }

        [DataMember]
        public string UserRole
        {    
            get;    
            set;    
        }
           [DataMember]
        public string Telephone
        {    
            get;    
            set;    
        }   
    }
}
aizaz
  • 3,056
  • 9
  • 25
  • 57
codespider
  • 81
  • 5
  • You may get helpfrom this link http://stackoverflow.com/questions/2086666/how-do-i-return-clean-json-from-a-wcf-service?rq=1 – Soma Sarkar May 14 '13 at 05:53

2 Answers2

0
<script type="text/javascript">
    $(document).ready(function () {
        alert("x");
        var content = 
"<table><tr style=\"background-color:Silver\"><th>Useid</th> <th>Empname</th><th>Username</th>" +
        "<th>Password</th><th>Userrole</th><th>EMailID</th><th>TELEPHONE</th></tr>";
        $.ajax({
            type: 'POST',
            url: 'service1.svc/SelectUserDetails/', 
            dataType: 'json', //Expected data format from server
            processdata: true,
            contentType: "application/json; charset=utf-8",
            data: '{}',

            success: function (data) {

                $.each(data, function (i, item) {

                    content += "<tr style=\"text-align:center\">";
                    content += "<td style=\"background-color:White\">" + data[i].UserID + "</td>";
                    content += "<td style=\"background-color:White\">" + data[i].EmpName + "</td>";
                    content += "<td style=\"background-color:White\">" + data[i].UserName + "</td>";
                    content += "<td style=\"background-color:White\">" + data[i].Password + "</td>";
                    content += "<td style=\"background-color:White\">" + data[i].UserRole + "</td>";
                    content += "<td style=\"background-color:White\">" + data[i].Email + "</td>";
                    content += "<td style=\"background-color:White\">" + data[i].Telephone + "</td>";
                    content += "</tr>";
                });
                content += "</table>";
                $('#sdf').html(content)
            }
             , error: function () {
                 alert("error again");
             }

        });

The url doesnot repond i guess while m calling...dont know wats d reason
codespider
  • 81
  • 5
  • @innovation : my question is hw i can consume this service in mvc view?? i tried the above method using jquery bt its going to the error function. – codespider May 14 '13 at 06:10
0

try the method without .svc file.m sure u wil gt a solution.:)

neetz
  • 140
  • 4
  • 11