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>
このエラーが発生する理由は何ですか? どうすれば修正できますか?
どんな助けでも彼は高く評価します。