我正在尝试将我的“用户”表的所有信息存储在 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;
}
}
}