【发布时间】:2012-06-26 01:54:43
【问题描述】:
我是 jQuery 和 jqGrid 的新手。我编写了以下代码来调用 WCF RESTful 服务并填充 jqGrid。虽然对 WCF RESTful 服务的调用返回一个 json 输出,但 jqGrid 出于某种原因无法解释此输出。
IService 接口:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(UriTemplate = "/Employees", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "GET")]
List<Employee> GetCollection();
}
[DataContract(Namespace="")]
public class Employee
{
[DataMember(IsRequired = true, Name = "empId", Order = 1)]
public string EmpId { get; set; }
[DataMember(IsRequired = false, Name = "empName", Order = 2)]
public string EmpName { get; set; }
[DataMember(IsRequired = false, Name = "dob", Order = 3)]
public DateTime DOB { get; set; }
[DataMember(IsRequired = false, Name = "department", Order = 4)]
public string Department { get; set; }
}
服务实现:
public List<Employee> GetCollection()
{
List<Employee> empList = new List<Employee>();
Employee emp = new Employee();
emp.EmpId = "1";
emp.DOB = Convert.ToDateTime("21/03/1979");
emp.EmpName = "Haris";
emp.Department = "HR";
empList.Add(emp);
return empList;
}
jQuery 脚本:
jQuery(document).ready(function() {
$("#jQGrid").html("<table id=\"list\"></table><div id=\"page\"></div>");
jQuery("#list").jqGrid({
url:'http://localhost:9002/SampleServices/Service/REST/Employees',
//datastr: mystr,
data: "{}", // For empty input data use "{}",
dataType: "json",
type: "GET",
contentType: "application/json; charset=utf-8",
colNames: ['Emp Id.', 'Emp Name', 'DOB', 'Department'],
colModel: [{ name: 'empId', index: 'empId', width: 90, sortable: true },
{ name: 'empName', index: 'empName', width: 130, sortable: false },
{ name: 'dob', index: 'dob', width: 100, sortable: false },
{ name: 'department', index: 'department', width: 180, sortable: false }
],
multiselect: false,
paging: true,
rowNum: 1,
rowList: [1, 5, 10],
pager: $("#page"),
loadonce: true,
caption: "Employee Details",
success: successFunction
}).navGrid('#page', { edit: false, add: false, del: false }
);
});
致电 “http://localhost:9002/SampleServices/Service/REST/Employees” 返回以下内容: [{"empId":"1","empName":"Haris","dob":"/Date(290851200000-0700)/","department":"HR"}]
开发者可以帮我解决这个问题吗?
【问题讨论】: