Error: Cannot process the message because the content type "application / json; charset = utf-8 was not the expected type

I try to call the WCF service using jQuery and get this error:

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

Here's what my WCF service looks like:

Interface:

 public interface IService { [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)] PhotoServiceResponse GetPhoto(); } [DataContract] public class PhotoServiceResponse { [MessageBodyMember] public Byte[] Photo { get; set; } } 

Implementation:

 [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 } 

Config:

 <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> 

This is how I try to access this service using 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> 

What is the reason why I get this error? How to fix it?

Any help would be greatly appreciated.

+4
source share
3 answers

I think you encountered some exception because you are making some errors in the binding and others.

For REST communication in WCF, you should use webHttpBinding , not wsHttpBinding . Secondly, you must mark the Photo DataMember property.

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

And you should use System.ServiceModel.Activation.WebServiceHostFactory in the svc file.

Ex.

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

Once you have fixed these problems, at least you see some kind of answer on the client side, and if the service is running in a different domain, make sure that you have made enough settings on the service side for cross-site communication.

+4
source

If I really read your code incorrectly, it looks like you are trying to return an Ajax file as a byte array. This is a couple of problems. First of all, as described in the jQuery API reference for Ajax return types , it seems that you cannot return files through Ajax using the built-in jQuery Ajax object. Second (and a bit more subjective), why don't you just return the URL to the file, especially if it's an image? Unless you have a good reason, I would just return the file url to your Ajax result.

+1
source

Try adding this on top of your class:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

I had the same problem and fixed it.

+1
source

All Articles