Nie można przetworzyć wiadomości, ponieważ typ treści „aplikacja / json; charset = utf-8 'nie był oczekiwanym typem' text / xml; charset = utf-8 '
Otrzymuję powyższą odpowiedź podczas wywoływania usługi WCF za pośrednictwem ajax json. Mój kod wywoławczy to:
<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>
Moja usługa to:
[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; }
}
Moja metoda to:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class UserService : IUserService
{
public Answer Calculate()
{
Answer answer = new Answer();
answer.answer="Hello World";
return answer;
}
}
Walczę już od jakiegoś czasu, widzę, że inni ludzie mają ten sam typ problemu i spróbowałem wszystkich sugestii, ale nadal nic nie działa.
Gdzie jest problem? Jak mogę to rozwiązać?