XMLHttpRequest post 调用 wcf 服务时出现 400 Bad request 错误

XMLHttpRequest post 400 Bad request error when calling wcf Service

您好,我正在 post 从 javascript 访问 wcf 服务。我可以 post 单个参数(字符串、blob、int)很好,但是当我尝试将数据放入 class 时,我收到 400 Bad Request 错误。我已经为我的 BodyStyle 尝试了 Bare 和 Wrapped,但每个都得到相同的错误。任何想法可能会发生什么?

谢谢

皮特

C# 数据协定:

 [DataContract]
    public class TestData
    {
       [DataMember]
        public string SubmissionID { get; set; }

    }

C# 接口:

 [OperationContract(Name = "Upload")]
        [DataContractFormat]
        [WebInvoke(Method = "POST",
                   UriTemplate = "Upload/",
                   BodyStyle = WebMessageBodyStyle.Wrapped,//Bare gives same error
                   ResponseFormat = WebMessageFormat.Json)]
        String Upload(TestData ps);

C#服务方法:

 public String Upload(TestData ps)
        {
....
return "Submission Complete";
}

Javascript 通话:

var TestData = {SubmissionID: "1" };
 var xhr = new XMLHttpRequest();
                xhr.open('POST', 'http://localhost:59070/WCFUploader.svc/Upload/', true);
xhr.send(TestData);//400 Bad Request

C# 网络配置:

 <?xml version="1.0"?>
    <configuration>

      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.6.1" />
        <httpRuntime targetFramework="4.6.1"/>
      </system.web>
      <system.serviceModel>
          <services>
              <service name="PhotoUploadServiceTest.WCFUploader" behaviorConfiguration="defaultServiceBehavior">
                  <endpoint address="" binding="webHttpBinding" behaviorConfiguration="defaultEndpointBehavior"

                     contract="PhotoUploadServiceTest.IWCFUploader" />
              </service>
          </services>
          <bindings>
              <webHttpBinding>
                  <binding maxBufferSize="2147483647"

                           maxBufferPoolSize="2147483647"

                           maxReceivedMessageSize="2147483647"

                           transferMode="Streamed"

                           sendTimeout="00:05:00">
                      <readerQuotas  maxDepth="2147483647"

                                     maxStringContentLength="2147483647"

                                     maxArrayLength="2147483647"

                                     maxBytesPerRead="2147483647"

                                     maxNameTableCharCount="2147483647"/>
                      <security mode="None" />
                  </binding>
              </webHttpBinding>
          </bindings>
          <behaviors>
              <endpointBehaviors>
                  <behavior name="defaultEndpointBehavior">
                      <webHttp/>
                  </behavior>
              </endpointBehaviors>
              <serviceBehaviors>
                  <behavior name="defaultServiceBehavior">
                      <serviceMetadata httpGetEnabled="true" />
                      <serviceDebug includeExceptionDetailInFaults="true" />
                  </behavior>
              </serviceBehaviors>
          </behaviors>

        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>

      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>

    </configuration>

问题出在我的 javascript 电话中。我需要将请求的内容 header 设置为 "application/json" 并且需要在发送之前创建 object 的字符串序列——使用了 dojo 库中的 dojo.toJson :

   var TestData = {SubmissionID: "1" };
   xhr.setRequestHeader("Content-type", "application/json");
   var xhr = new XMLHttpRequest();
            xhr.open('POST', 
   'http://localhost:59070/WCFUploader.svc/Upload/', true);
  xhr.send(dojo.toJson(TestData));//this worked!!