C# WCF Restful在POST上收到错误请求
我有四种服务方式:三种是GET,另一种是POST。我的问题是调用此POST方法。当我调用这个方法时,我总是收到404错误的请求。如果我在没有主体的情况下调用此方法,则有效 我的代码:C# WCF Restful在POST上收到错误请求,c#,json,wcf,post,C#,Json,Wcf,Post,我有四种服务方式:三种是GET,另一种是POST。我的问题是调用此POST方法。当我调用这个方法时,我总是收到404错误的请求。如果我在没有主体的情况下调用此方法,则有效 我的代码: MyService.svc.cs [ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public
MyService.svc.cs
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "user/{idUser}/garages")]
public GaragesResource GetGarages(string idUser)
{
[...]
}
[...]
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "test")]
public BaseResource PostChecklist(BaseResource baseResource)
{
[...]
}
}
namespace ChecklistService.Resources
{
[DataContract]
public class BaseResource
{
[DataMember]
public bool Sucess { get; set; }
[DataMember]
public string Message { get; set; }
public BaseResource()
{
}
}
}
Web.config
[...]
<system.serviceModel>
<services>
<service name="ChecklistService.MyService">
<endpoint address="" behaviorConfiguration="ChecklistService.ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="ChecklistService.MyService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="ChecklistService.ServiceAspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
[...]
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
[...]
我已经读到,我不需要显式地显示序列化注释,但我使用它是为了确保这不是问题所在
现在,客户端调用:
var data = {
"Sucess":true,
"Message":"Test"
}
/*var data = {
"baseResource": {
"Sucess":true,
"Message":"Test"
}
}*/
$.ajax({
type: "POST",
url: "http://localhost/Checklist/MyService/test",
data: JSON.stringify(data),
contentType: "text/plain",
dataType: "json",
processData: false,
success: function (data, status, jqXHR) {
alert("success…" + data);
},
error: function (xhr) {
//alert(xhr.responseText);
}
});
contentType是这样设置的,因为jquery文档告诉我们,跨域调用只接受3种类型的contentType,否则请求的方法会改变。因此,如果我使用“application/json”,请求的方法将是OPTIONS,而不是POST
我一直在尝试以多种不同的方式制作数据,但什么都没有。唯一有效的方法是发送一个空字符串
我忘了什么?还是我搞错了?请帮忙
谢谢大家!