【发布时间】:2011-08-28 07:30:23
【问题描述】:
我正在尝试通过使用 REST、WCF 和 JSON(所有这些技术的新手)让我的应用程序正常工作。我的“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); }
});
这里是配置的东西:
<services>
<service behaviorConfiguration="ServiceBehaviour" name="ContactCompanyService">
<endpoint address="contactcompanies" behaviorConfiguration="web" binding="webHttpBinding" contract="IContactCompanyService"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
这是合同:
[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,这导致对象不为空……但对象的每个属性都为空。也就是说,wrapped 是我想走的路。
如有任何帮助,我将不胜感激。如果您需要更多信息,请告诉我。
更新
我从头开始一个全新的项目 - 剥离。
我得到完全相同的结果 - 当 WCF 代码接收到该对象时,该对象为空。
这是我在这个新测试项目中所做的。
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 };
}
}
网站项目。 服务.svc:
<%@ ServiceHost Service="NullTestService.Service1" %>
web项目中的web.config:
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="NullTestService.Service1">
<endpoint address="nulltestaddress" behaviorConfiguration="web" binding="webHttpBinding" contract="NullTestService.IService1"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
最后是 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;
}
(“返回”行)NullTestTypeObject 始终为 NULL。
我将非常感谢您的帮助。我为此浪费了很多时间。
【问题讨论】:
-
输入名称是“NullTestTypeObject”,这是您需要在数据中使用的名称(数据:'{"NullTestTypeObject":{...)跨度>
-
@carlosfigueira 这似乎不起作用。