【问题标题】:Error: Cannot process the message because the content type 'application/json; charset=utf-8 was not the expected type错误:无法处理消息,因为内容类型为 'application/json; charset=utf-8 不是预期的类型
【发布时间】:2012-06-14 18:19:56
【问题描述】:

我正在尝试使用 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>

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

他将非常感谢任何帮助。

【问题讨论】:

    标签: jquery asp.net ajax wcf .net-3.5


    【解决方案1】:

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

    【讨论】:

    • 我没有返回文件。我正在返回我猜传输应该没有问题的字节。而且我无法发送文件的 URL,因为文件驻留在本地系统上,我的 WCF 服务也是如此。
    • 如果您无法使资源可用,您可以尝试这种技术。 stackoverflow.com/questions/8734620/…
    【解决方案2】:

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

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

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

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

    例如。

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

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

    【讨论】:

      【解决方案3】:

      尝试将其添加到您的班级之上:

      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      

      我遇到了同样的问题,它已经解决了。

      【讨论】:

        猜你喜欢
        • 2013-01-02
        • 2014-12-24
        • 1970-01-01
        • 2019-05-28
        • 2020-04-25
        • 1970-01-01
        • 2020-03-20
        • 2014-10-25
        • 1970-01-01
        相关资源
        最近更新 更多