Ho ottenuto la risposta di cui sopra quando si chiama un servizio WCF tramite ajax json. Il mio codice chiamante è:Impossibile elaborare il messaggio perché il tipo di contenuto 'application/json; charset = utf-8 'non era il tipo previsto' text/xml; charset = utf-8 '
<script type="text/javascript">
$(document).ready(function() {
$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:90/WebServices/UserService.svc/Calculate",
data: "{}",
timeout: 10000,
dataType: "json",
success: function (response) {
alert(response)
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(thrownError);
}
});
});
</script>
Il mio servizio è:
[ServiceContract]
public interface IUserService
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json
)]
Answer Calculate();
}
[DataContract]
public class Answer
{
[DataMember]
public string answer { get; set; }
}
Il mio metodo è:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class UserService : IUserService
{
public Answer Calculate()
{
Answer answer = new Answer();
answer.answer="Hello World";
return answer;
}
}
Sono stato in lotta con per qualche tempo, vedo altre persone hanno avuto lo stesso tipo problema e ho provato tutti i suggerimenti lì, ma ancora non funziona nulla.
Dov'è il problema? Come posso risolverlo?
Ciao, quali suggerimenti hai provato? –