-1

我正在尝试将我的“用户”表的所有信息存储在 wcf 中。并以 json 格式返回。当我调用服务时,我可以看到实体。但我的问题是如何在我看来调用此服务。我尝试过使用 ajax 调用它,但所发出的只是错误消息:( 请帮助

服务.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;    
        }   
    }
}
4

2 回答 2

0

尝试没有 .svc 文件的方法。我确定你会得到一个解决方案。:)

于 2013-05-22T09:57:53.060 回答
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
于 2013-05-14T04:39:20.387 回答