我正在尝试使用REST、WCF和JSON (所有这些技术的新手)来让我的应用程序正常工作。我让“GET”运行得很好。这是“帖子”给我带来的问题。
正如您将在下面看到的,我使用JSON.stringify“打包”我的JSON,然后向REST资源发送帖子。但是,当对象到达处理请求的WCF方法时,该对象始终为空。
代码如下:
$.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,这将导致对象不为空...但是对象的每个属性都是空的。这就是说,包装是我想要的方式。
如果有任何帮助,我将不胜感激。如果您需要进一步的信息,请告诉我。
更新
我从头开始做了一个全新的项目-- back。
我得到了完全相同的结果-对象,当被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 };
}
}
网站项目。Service.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;
}
( 'return‘行) NullTestTypeObject始终为空。
如果能帮上忙,我将非常感激。我在这件事上损失了很多时间。
https://stackoverflow.com/questions/6049454
复制相似问题