我正在尝试将我的“用户”表的所有信息存储在wcf中。并以json格式返回。当我调用服务时,我可以看到实体。 但我的问题是如何在我的视图中调用此服务。我已尝试使用ajax调用但所有拍摄的是错误消息:( plzz帮助
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;
}
}
}
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;
}
}
}
答案 0 :(得分: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
答案 1 :(得分:0)
尝试没有.svc文件的方法。确定你将解决方案。:)