Jquery 错误:无法处理消息,因为内容类型为';应用程序/json;charset=utf-8不是预期的类型

Jquery 错误:无法处理消息,因为内容类型为';应用程序/json;charset=utf-8不是预期的类型,jquery,asp.net,ajax,wcf,.net-3.5,Jquery,Asp.net,Ajax,Wcf,.net 3.5,我试图使用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 { [Ope

我试图使用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>

$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“http://localhost:8081/Service/GetPhoto",
数据:“{}”,
跨域:是的,
数据类型:“json”,
成功:功能(msg){
警报(msg);
},
错误:函数(jqXHR)
{
警报(jqXHR.statusText);
}
});
我出现这个错误的原因是什么?我怎么修理它


他将非常感谢您的任何帮助。

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

我认为您面临一些异常,因为您在绑定和其他方面犯了一些错误

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

  [DataContract]
  public class PhotoServiceResponse
  {
    [DataMember]
    public Byte[] Photo { get; set; }
  }
您必须在svc文件中使用
System.ServiceModel.Activation.WebServiceHostFactory



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

尝试将此添加到类的顶部:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

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

我不返回文件。我返回的字节,我想应该没有问题传输。我无法发送文件的URL,因为文件驻留在本地系统上,我的WCF服务也是如此。如果您无法使资源可用,您可以尝试这种技术。
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]