调用wcf服务时,XMLHttpRequest发布400错误请求错误

时间:2018-04-19 20:12:37

标签: javascript c# wcf wcf-binding

您好我是从javascript发布到wcf服务的。我可以发布一个单独的参数(string,blob,int),但是当我尝试将数据放入一个类时,我得到一个400 Bad Request错误。我为BodyStyle尝试了Bare和Wrap,但每个都得到了同样的错误。有什么想法会发生什么?

由于

皮特

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#Web配置:

 <?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>

1 个答案:

答案 0 :(得分:0)

问题出在我的javascript调用中。我需要将请求标头的内容设置为“application / json”,并且需要在发送之前创建对象的String序列 - 从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!!