WCF Restful在POST上收到错误请求

时间:2014-12-11 00:27:13

标签: c# json wcf post

我在服务中有四种方法:其中三种是GET,另一种是POST。我的问题是调用这个POST方法。当我调用此方法时,我总是得到404 Bad Request。如果我在没有身体的情况下调用此方法,则可以正常工作。

我的代码:

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)
    {
        [...]
    }
}

的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>
[...]

BaseResource.cs

namespace ChecklistService.Resources
{
    [DataContract]
    public class BaseResource
    {
        [DataMember]
        public bool Sucess { get; set; }
        [DataMember]
        public string Message { get; set; }

        public BaseResource()
        {
        }
    }
}

我已经读过我不需要明确序列化注释,但我确保这不是问题所在。

现在,客户致电:

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或请求的方法会改变。因此,如果我使用&#34; application / json&#34;,请求的方法将是OPTIONS,而不是POST。

我一直试图以多种不同的方式制作数据。唯一有效的方法是发送一个空字符串。

我忘记了什么?或者我说错了什么?请帮忙。

谢谢!

0 个答案:

没有答案