为什么在WCF服务中发送的JSON数据为空

时间:2015-10-14 15:14:20

标签: jquery json ajax wcf rest

这是一个CORS restful wcf。除了POST之外,GET和DELETE的方法就像奶油一样。 POST数据能够传输到EmployeeService.svc.cs,但我得到的值是null或default。

DataModel.cs

[DataContract]
public class Employee
{
    [DataMember]
    public int EmpId { get; set; }
    [DataMember]
    public string Fname { get; set; }
    [DataMember]
    public string Lname { get; set; }
    [DataMember ]
    public DateTime JoinDate { get; set; }
    [DataMember]
    public int Age { get; set; }
    [DataMember]
    public int Salary { get; set; }
    [DataMember]
    public string Designation { get; set; }
}

IEmployeeService.cs

[WebInvoke(Method = "POST", UriTemplate = "EmployeePOST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Bare)]
[OperationContract]
void AddEmployee(Employee newEmp);

EmployeeService.svc.cs

    public void AddEmployee(Employee newEmp)
    {
        EmployeeData.Instance.Add(newEmp);
    }

Default.aspx的

  function POSTMethodCall() {
       var EmpUser = [{ "EmpId": "13", "Fname": "WebClientUser", "Lname": "Raju", "JoinDate": "", "Age": "23", "Salary": "12000", "Designation": "Software Engineer"}];
       var st = JSON.stringify(EmpUser);
       //var msg = { "EmpId": "13", "Fname": "WebClientUser", "Lname": "Raju", "JoinDate": "", "Age": "23", "Salary": "12000", "Designation": "Software Engineer" };
       $.ajax({
          cache:false,
          type: "POST",
          async:false,
          url: "http://localhost:8090/EmployeeService.svc/MyService/EmployeeService/EmployeePOST",
          data: st,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (data) {
              // Play with response returned in JSON format
          },
          error:ServiceFailed
      });

  }

EmployeeService.svc.cs

enter image description here

json数据格式错了吗?实际问题是什么?我花了2天时间找到解决方案,但仍然失败..请帮助..

0 个答案:

没有答案