将JSON发送到WCF Rest Service – 对象始终为null

我试图通过使用REST,WCF和JSON(所有这些技术的新function)来使我的应用程序正常工作。 我让’GET’工作得很好。 正是“POST”导致了我的问题。

正如您将在下面看到的,我使用JSON.stringify打包’我的JSON,然后将POST发送到REST资源。 但是,当对象到达处理请求的WCF方法时,该对象始终为null。

这是代码:

$.ajax({ type: "POST", dataType: "json", url: "Services/ContactCompanyService.svc/contactcompanies/customers", contentType: "application/json; charset=utf-8", data: JSON.stringify({ contactcompany: newCustomer }), success: function (html) { alert(html); } }); 

这是配置的东西:

                         

这是合同:

  [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "customers")] [return: MessageParameter(Name = "ContactCompany")] ContactCompany AddContactCompany(ContactCompany ContactCompanyObject); 

它是实现上述接口的方法,其中ContactCompanyObject为null。

我究竟做错了什么? 请不要排除我的愚蠢。

进一步:我将WebMessageBodyStyle更改为.Bare,这导致对象不为null …但对象的每个属性都为null。 也就是说,包裹是我想要的方式。

我将不胜感激任何帮助。 如果您需要更多信息,请与我们联系。

UPDATE

我从头开始用一个全新的项目 – 剥离了。

我得到了完全相同的结果 – 当WCF代码接收时,该对象为null。

这就是我在这个新测试项目上所做的。

WCF合同:

(在命名空间下:NullTestService

 [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "NullTestPost")] [return: MessageParameter(Name = "NullTestType")] NullTestType GettMethod(); [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "NullTestPost")] [return: MessageParameter(Name = "NullTestType")] NullTestType PostMethod(NullTestType NullTestTypeObject); } [DataContract] public class NullTestType { [DataMember] public string NullTestString { get; set; } [DataMember] public int NullTestInt { get; set; } } 

服务实现:(相同的命名空间)

  public class Service1 : IService1 { public NullTestType PostMethod(NullTestType NullTestTypeObject) { return NullTestTypeObject; } public NullTestType GettMethod() { return new NullTestType { NullTestString = "Returned String", NullTestInt = 25 }; } } 

网站项目。 Service.svc:

  

web项目中的web.config:

                      

最后是web项目中的jQuery:

 $(function () { // $.ajax({ // type: "GET", // url: "http://localhost:8080/TestWeb/Service.svc/nulltestaddress/nulltestpost", // success: alertResult // }); alert('about to do it'); $.ajax({ type: "POST", url: "http://localhost:8080/TestWeb/Service.svc/nulltestaddress/nulltestpost", dataType: "json", contentType: "application/json; charset=utf-8", data: '{"NullTestType":{"NullTestString":"This is a post string","NullTestInt":25}}', success: alertResult }); }); function alertResult(data) { alert(data.NullTestType.NullTestString); } 

所以。 (注释掉)GET工作正常并返回JSON。 POST没有。 在线上:

 public NullTestType PostMethod(NullTestType NullTestTypeObject) { return NullTestTypeObject; } 

(’return’行)NullTestTypeObject始终为NULL。

我非常感谢你的帮助。 我已经失去了很多时间。

如果Wrapped是你想要做的,那么你需要在操作参数名称中包装请求:

 var input = { "ContactCompanyObject" : newCustomer }; $.ajax({ data: input ... }); 

或者对于第二个示例,如果将ajax调用更改为下面显示的调用,则应获得预期结果:

 var input = { NullTestTypeObject: { NullTestString: "Hello", NullTestInt: 123} }; alert("Input: " + JSON.stringify(input)); $.ajax({ type: "POST", url: "./Service1.svc/nulltestaddress/NullTestPost", contentType: "application/json", data: JSON.stringify(input), success: function (result) { alert("POST result: " + JSON.stringify(result)); } });