4

我正在尝试使用 jQuery 调用 WCF 服务并收到此错误:

"Cannot process the message because the content type 
'application/json; charset=utf-8' was not the expected type 'multipart/related; 
type="application/xop+xml"'."

这就是我的 WCF 服务的样子:

界面:

public interface IService
{

    [OperationContract]
    [WebInvoke(Method = "POST",
      ResponseFormat = WebMessageFormat.Json)]
    PhotoServiceResponse GetPhoto();

}

[DataContract]
public class PhotoServiceResponse
{
    [MessageBodyMember]
    public Byte[] Photo { get; set; }
}

执行:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
    #region IService Members


    public PhotoServiceResponse GetPhoto()
    {
        PhotoServiceResponse response = new PhotoServiceResponse();
        response.Photo = File.ReadAllBytes(@"C:\Temp\SomePic.bmp");
        return response; 
    }

    #endregion
}

配置:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WsHttpMtomBinding" maxReceivedMessageSize="5242880" messageEncoding="Mtom">
          <readerQuotas maxStringContentLength="655360" maxArrayLength="1310720" maxNameTableCharCount="1310720" maxBytesPerRead="327680" />
        </binding>

      </wsHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="svcBehaviour">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

    <services>
      <service behaviorConfiguration="svcBehaviour"
               name="Service">
        <endpoint address="" binding="wsHttpBinding"
           contract="IService"
           bindingConfiguration="WsHttpMtomBinding"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8081/Service" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

这就是我尝试使用 jQuery AJAX 访问此服务的方式:

   <script type="text/javascript">
    $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "http://localhost:8081/Service/GetPhoto",
      data: "{}",
      crossDomain: true,
      dataType: "json",
      success: function (msg) {
            alert(msg);
      },
      error: function(jqXHR)
      {
        alert(jqXHR.statusText);
      }
    });

    </script>

我收到此错误的原因是什么?我如何解决它?

他将高度赞赏任何帮助。

4

3 回答 3

4

我认为你正面临一些例外,因为你在绑定和其他方面犯了一些错误。

对于 WCF 中的 REST 通信,您应该使用webHttpBindingand not wsHttpBinding。其次,您应该Photo用 标记该属性DataMember

  [DataContract]
  public class PhotoServiceResponse
  {
    [DataMember]
    public Byte[] Photo { get; set; }
  }

而且你必须System.ServiceModel.Activation.WebServiceHostFactory在 svc 文件中使用。

前任。

<%@ ServiceHost Service="Service1" 
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

一旦你解决了这个问题,至少你会在客户端看到一些响应,如果服务在不同的域中运行,请确保你在服务端为跨站点通信做了足够的设置。

于 2012-06-12T08:06:39.833 回答
1

除非我真的误读了您的代码,否则您似乎试图通过 Ajax 将文件作为字节数组返回。这带来了几个问题。首先,如 Ajax 返回类型的 jQuery API 参考中所述,您似乎无法使用 jQuery 的内置 Ajax 对象通过 Ajax 返回文件。其次(更主观一点),为什么不直接返回文件的 URL,特别是如果它恰好是图像?除非您有令人信服的理由,否则我只会在您的 Ajax 结果中返回文件 URL。

于 2012-06-11T20:34:52.793 回答
1

尝试将其添加到您的课程之上:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

我有同样的问题,它解决了它。

于 2013-05-21T11:06:26.400 回答