我正在尝试使用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>
我收到此错误的原因是什么?我该如何解决?
任何帮助都会受到高度赞赏。
答案 0 :(得分:4)
我认为你面临一些例外,因为你在绑定和其他方面犯了一些错误。
对于WCF中的REST通信,您应该使用webHttpBinding
而不是wsHttpBinding
。其次,您应该使用Photo
标记DataMember
属性。
[DataContract]
public class PhotoServiceResponse
{
[DataMember]
public Byte[] Photo { get; set; }
}
您必须在svc文件中使用System.ServiceModel.Activation.WebServiceHostFactory
。
实施例。
<%@ ServiceHost Service="Service1"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
一旦您解决了这个问题,至少您会在客户端看到一些响应,如果服务在不同的域中运行,请确保您已在服务端进行了足够的设置以进行跨站点通信。
答案 1 :(得分:1)
除非我真的误读了你的代码,否则看起来你试图将Ajax文件作为字节数组返回。这提出了一些问题。首先,as described in the jQuery API reference for Ajax return types,看来你无法使用jQuery的内置Ajax对象通过Ajax返回文件。第二个(也有点主观),你为什么不直接返回文件的URL,特别是如果它恰好是一个图像?除非你有令人信服的理由,否则我只会在Ajax结果中返回文件URL。
答案 2 :(得分:1)
尝试在课堂上添加此内容:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
我遇到了同样的问题并修好了。